diff --git a/P8Coder/CoderForm.cs b/P8Coder/CoderForm.cs index ed85ab2..a476632 100644 --- a/P8Coder/CoderForm.cs +++ b/P8Coder/CoderForm.cs @@ -1,23 +1,14 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; -using System.Linq; -using System.Resources; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; using P8Coder.Core; using P8Coder.Utils; -using P8Coder.Controls; using System.Runtime.InteropServices; using System.Threading; -using System.Drawing.Drawing2D; namespace P8Coder { @@ -33,9 +24,6 @@ public partial class CoderForm : Form private string title = "P8Coder"; private Dictionary pico8help; - //private Process pico8Process; - //private IntPtr pico8Handle; - private SpritesheetForm spritesheetForm; private MapForm mapForm; @@ -257,7 +245,8 @@ private void updateProject() if (currentProject.CartFilename != cartInput.Text) { currentProject.CartFilename = cartInput.Text; - currentProject.Cart.OnChangeCallback = new Action(reloadCartridge); + if (currentProject.Cart == null) MessageBox.Show("Failed to load the cartridge!", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error); + else currentProject.Cart.OnChangeCallback = new Action(reloadCartridge); } saveBtn.Enabled = true; } diff --git a/P8Coder/Core/Cartridge.cs b/P8Coder/Core/Cartridge.cs index e0b0e49..c9753be 100644 --- a/P8Coder/Core/Cartridge.cs +++ b/P8Coder/Core/Cartridge.cs @@ -73,33 +73,33 @@ public void Load() private void setupFromText(string text) { - char[] charsToTrim = { '\r', '\n', ' ' }; - rawText = text.Replace("\r", ""); - string[] sections = Regex.Split(rawText, @"__(?:lua|gfx|gff|map|sfx|music)__\n"); - - if (sections.Length != 7) throw new Exception("Invalid file format! (1)"); - string[] header = sections[0].Trim().Split('\n'); - if (header.Length != 2) throw new Exception("Invalid file format! (2)"); + string[] lines = rawText.Trim().Split('\n'); - if (!readHeader(header[0].Trim())) throw new Exception("Invalid file format! (3)"); - if (!readVersion(header[1].Trim())) throw new Exception("Invalid file format! (4)"); + if (!readHeader(lines[0].Trim())) throw new Exception("Invalid file format! (1)"); + if (!readVersion(lines[1].Trim())) throw new Exception("Invalid file format! (2)"); - Lua = sections[1].Trim(); - Gfx = sections[2].Replace("\n", "").Trim(); + Lua = getSection("__lua__", rawText).Trim(); + Gfx = getSection("__gfx__", rawText).Replace("\n", "").Trim(); - if (Gfx.Length != 16384 /*16511*/) throw new Exception("Invalid file format! (5)"); + if (Gfx.Length != 16384) + throw new Exception("Invalid file format! (3)"); - Gff = sections[3].Trim(); - Map = sections[4].Replace("\n", "").Trim(); - Sfx = sections[5].Trim(); - Music = sections[6].Trim(); + Map = getSection("__map__", rawText).Replace("\n", "").Trim(); createSprites(); createMapSprite(); } + private string getSection(string name, string data) + { + int start = data.IndexOf(name) + name.Length; + int end = data.IndexOf("__", start + name.Length); + + return data.Substring(start, end - start).Trim(); + } + private void createSprites() { if (SpriteSheet == null) @@ -189,10 +189,12 @@ private bool readVersion(string text) { //NOTE: version changes with every release of pico-8 // This means that we have no simple check to see if the file is compatible. - //if (text != "version 7") return false; + + string[] parts = text.Split(' '); + if (parts.Length != 2) return false; int v = 0; - if (!int.TryParse(text.Substring(text.Length - 1, 1), out v)) return false; + if (!int.TryParse(parts[1], out v)) return false; Version = v; return true; }