private void btnAddCharset_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Title = "Open charset project"; openFile.Filter = "Charset Project Files|*.charsetproject"; if (openFile.ShowDialog() == DialogResult.OK) { Types.CharsetProject charSet = main.OpenCharsetProject(openFile.FileName); if (charSet != null) { charSet.Name = openFile.FileName; string shortName = System.IO.Path.GetFileNameWithoutExtension(openFile.FileName); main.m_Project.Charsets.Add(charSet); Types.CharsetProjectInfo info = new ElementEditor.Types.CharsetProjectInfo(); info.Filename = charSet.Name; info.Multicolor = true; main.m_Project.CharsetProjects.Add(info); main.comboScreenCharset.Items.Add(shortName); main.comboElementCharset.Items.Add(shortName); listCharsets.Items.Add(shortName); } } }
public bool LoadFromFile(string Filename) { Elements.Clear(); ObjectTemplates.Clear(); this.Screens.Clear(); Regions.Clear(); GR.Memory.ByteBuffer editorFile = GR.IO.File.ReadAllBytes(Filename); if (editorFile == null) { return(false); } GR.IO.MemoryReader memIn = editorFile.MemoryReader(); GR.IO.FileChunk chunk = new GR.IO.FileChunk(); while (chunk.ReadFromStream(memIn)) { GR.IO.MemoryReader memReader = chunk.MemoryReader(); switch (chunk.Type) { case (ushort)ChunkType.GENERAL: // general { OldCharsetProjectFilename = memReader.ReadString(); ExportFilename = memReader.ReadString(); ExportPrefix = memReader.ReadString(); ExportConstantOffset = memReader.ReadInt32(); SpriteProjectFilename = memReader.ReadString(); ProjectType = memReader.ReadString(); EmptyChar = memReader.ReadUInt8(); EmptyColor = memReader.ReadUInt8(); } break; case (ushort)ChunkType.CHARSET_INFO: { Types.CharsetProjectInfo info = new ElementEditor.Types.CharsetProjectInfo(); info.Filename = memReader.ReadString(); info.Multicolor = (memReader.ReadUInt8() == 1); CharsetProjects.Add(info); } break; case (ushort)ChunkType.ELEMENT: // element { Element element = new Element(); element.Name = memReader.ReadString(); element.Index = memReader.ReadInt32(); int w = memReader.ReadInt32(); int h = memReader.ReadInt32(); element.Characters.Resize(w, h); for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { element.Characters[i, j].Char = memReader.ReadUInt8(); element.Characters[i, j].Color = memReader.ReadUInt8(); } } element.CharsetIndex = memReader.ReadInt32(); //element.CharsetIndex = 0; if ((CharsetProjects.Count == 0) && (element.CharsetIndex != 0)) { element.CharsetIndex = 0; } else if (element.CharsetIndex >= CharsetProjects.Count) { element.CharsetIndex = 0; } Elements.Add(element); } break; case (ushort)ChunkType.OLD_SCREEN: // screen { Screen screen = new Screen(); screen.Name = memReader.ReadString(); int numElements = memReader.ReadInt32(); for (int i = 0; i < numElements; ++i) { ScreenElement element = new ScreenElement(); element.Type = (ScreenElementType)memReader.ReadInt32(); int elementIndex = memReader.ReadInt32(); if (elementIndex >= Elements.Count) { elementIndex = 0; } element.Index = elementIndex; element.X = memReader.ReadInt32(); element.Y = memReader.ReadInt32(); element.Repeats = memReader.ReadInt32(); element.Char = memReader.ReadInt32(); element.Color = memReader.ReadInt32(); element.SearchObjectIndex = memReader.ReadInt32(); screen.DisplayedElements.Add(element); } // fetch extra object data foreach (ScreenElement element in screen.DisplayedElements) { if (element.Type == ScreenElementType.LD_OBJECT) { GR.IO.FileChunk chunkObject = new GR.IO.FileChunk(); if (!chunkObject.ReadFromStream(memReader)) { return(false); } element.Object = new GameObject(); GR.IO.MemoryReader memObjectReader = chunkObject.MemoryReader(); element.Object.TemplateIndex = memObjectReader.ReadInt32(); element.Object.X = memObjectReader.ReadInt32(); element.Object.Y = memObjectReader.ReadInt32(); element.Object.Color = memObjectReader.ReadInt32(); element.Object.Speed = memObjectReader.ReadInt32(); element.Object.Behaviour = memObjectReader.ReadInt32(); element.Object.MoveBorderLeft = memObjectReader.ReadInt32(); element.Object.MoveBorderTop = memObjectReader.ReadInt32(); element.Object.MoveBorderRight = memObjectReader.ReadInt32(); element.Object.MoveBorderBottom = memObjectReader.ReadInt32(); element.Object.Data = memObjectReader.ReadInt32(); } else if (element.Type == ScreenElementType.LD_SPAWN_SPOT) { GR.IO.FileChunk chunkSpawn = new GR.IO.FileChunk(); if (!chunkSpawn.ReadFromStream(memReader)) { return(false); } element.Object = new GameObject(); GR.IO.MemoryReader memObjectReader = chunkSpawn.MemoryReader(); element.Object.TemplateIndex = memObjectReader.ReadInt32(); element.Object.X = memObjectReader.ReadInt32(); element.Object.Y = memObjectReader.ReadInt32(); element.Repeats = memObjectReader.ReadInt32(); } } GR.IO.FileChunk chunkMoreData = new GR.IO.FileChunk(); while (chunkMoreData.ReadFromStream(memReader)) { GR.IO.MemoryReader moreDataReader = chunkMoreData.MemoryReader(); switch (chunkMoreData.Type) { case (ushort)ChunkType.OLD_SCREEN_MORE_DATA: screen.Width = moreDataReader.ReadInt32(); break; } } Screens.Add(screen); } break; case (ushort)ChunkType.MAP_REGION: { Region region = new Region(); GR.IO.FileChunk subChunk = new GR.IO.FileChunk(); while (subChunk.ReadFromStream(memReader)) { GR.IO.MemoryReader subReader = subChunk.MemoryReader(); switch (subChunk.Type) { case (ushort)ChunkType.MAP_REGION_INFO: region.DisplayX = subReader.ReadInt32(); region.DisplayY = subReader.ReadInt32(); region.Vertical = (subReader.ReadInt32() != 0); { uint numExtraDataBytes = subReader.ReadUInt32(); region.ExtraData = new GR.Memory.ByteBuffer(); subReader.ReadBlock(region.ExtraData, numExtraDataBytes); } break; case (ushort)ChunkType.MAP_REGION_SCREEN: { RegionScreenInfo screenInfo = new RegionScreenInfo(); screenInfo.ScreenIndex = subReader.ReadInt32(); screenInfo.ExitN = (subReader.ReadInt32() != 0); screenInfo.ExitS = (subReader.ReadInt32() != 0); screenInfo.ExitW = (subReader.ReadInt32() != 0); screenInfo.ExitE = (subReader.ReadInt32() != 0); region.Screens.Add(screenInfo); } break; } } Regions.Add(region); } break; case (ushort)ChunkType.SCREEN: // screen { Screen screen = new Screen(); GR.IO.FileChunk subChunk = new GR.IO.FileChunk(); while (subChunk.ReadFromStream(memReader)) { GR.IO.MemoryReader subReader = subChunk.MemoryReader(); switch (subChunk.Type) { case (ushort)ChunkType.SCREEN_INFO: screen.Name = subReader.ReadString(); screen.Width = subReader.ReadInt32(); screen.Height = subReader.ReadInt32(); if (screen.Height == 0) { screen.Height = 25; } screen.ConfigByte = subReader.ReadUInt8(); screen.CharsetIndex = subReader.ReadInt32(); screen.WLConfigByte = subReader.ReadUInt8(); screen.ExtraData = subReader.ReadString(); screen.OverrideMC1 = subReader.ReadInt32() - 1; screen.OverrideMC2 = subReader.ReadInt32() - 1; break; case (ushort)ChunkType.SCREEN_ELEMENT: { ScreenElement element = new ScreenElement(); element.Type = (ScreenElementType)subReader.ReadInt32(); int elementIndex = subReader.ReadInt32(); if (elementIndex >= Elements.Count) { elementIndex = 0; } element.Index = elementIndex; element.X = subReader.ReadInt32(); element.Y = subReader.ReadInt32(); element.Repeats = subReader.ReadInt32(); element.Repeats2 = subReader.ReadInt32(); element.Char = subReader.ReadInt32(); element.Color = subReader.ReadInt32(); element.SearchObjectIndex = subReader.ReadInt32(); element.TargetX = subReader.ReadInt32(); element.TargetY = subReader.ReadInt32(); element.TargetLevel = subReader.ReadInt32(); element.Flags = subReader.ReadInt32(); if ((element.Type == ScreenElementType.LD_OBJECT) || (element.Type == ScreenElementType.LD_SPAWN_SPOT)) { element.Object = new GameObject(); } screen.DisplayedElements.Add(element); } break; case (ushort)ChunkType.ELEMENT_OBJECT_DATA: { ScreenElement screenElement = screen.DisplayedElements[subReader.ReadInt32()]; if (screenElement.Type == ScreenElementType.LD_OBJECT) { screenElement.Object.TemplateIndex = subReader.ReadInt32(); screenElement.Object.X = subReader.ReadInt32(); screenElement.Object.Y = subReader.ReadInt32(); screenElement.Object.Color = subReader.ReadInt32(); screenElement.Object.Speed = subReader.ReadInt32(); screenElement.Object.Behaviour = subReader.ReadInt32(); screenElement.Object.MoveBorderLeft = subReader.ReadInt32(); screenElement.Object.MoveBorderTop = subReader.ReadInt32(); screenElement.Object.MoveBorderRight = subReader.ReadInt32(); screenElement.Object.MoveBorderBottom = subReader.ReadInt32(); screenElement.Object.Data = subReader.ReadInt32(); screenElement.Object.OptionalValue = subReader.ReadInt32(); screenElement.Object.Optional = (GameObject.OptionalType)subReader.ReadInt32(); } } break; case (ushort)ChunkType.ELEMENT_SPAWN_SPOT_DATA: { ScreenElement screenElement = screen.DisplayedElements[subReader.ReadInt32()]; screenElement.Object.TemplateIndex = subReader.ReadInt32(); screenElement.Object.X = subReader.ReadInt32(); screenElement.Object.Y = subReader.ReadInt32(); screenElement.Repeats = subReader.ReadInt32(); } break; } } // fetch extra object data Screens.Add(screen); } break; case (ushort)ChunkType.OBJECT_TEMPLATE: { ObjectTemplate obj = new ObjectTemplate(); obj.Name = memReader.ReadString(); obj.StartSprite = memReader.ReadInt32(); int countBehaviours = memReader.ReadInt32(); for (int i = 0; i < countBehaviours; ++i) { int behaviourIndex = memReader.ReadInt32(); string behaviourName = memReader.ReadString(); int newIndex = obj.Behaviours.Count; obj.Behaviours[newIndex] = new Behaviour(); obj.Behaviours[newIndex].Name = behaviourName; obj.Behaviours[newIndex].Value = behaviourIndex; } ObjectTemplates.Add(obj); } break; } } return(true); }