Last active
January 26, 2021 16:26
-
-
Save infval/d3403dad72b7caaba001b98ee3c8f056 to your computer and use it in GitHub Desktop.
Import TMX - CadEditor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CadEditor; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Xml.Linq; | |
using System.Windows.Forms; | |
public class Script | |
{ | |
public void Execute(FormScript formScript) | |
{ | |
formScript.writeLog("########"); | |
formScript.writeLog("<< Import TMX to current screen - v1.2 >>"); | |
string defaultDir = Path.GetFullPath(ConfigScript.ConfigDirectory); // Normalization | |
string pathTMX = OpenFileDialogTMX(defaultDir, "CadLevel.tmx"); | |
if (pathTMX == "") | |
{ | |
formScript.writeLog("[Info] Canceled"); | |
return; | |
} | |
formScript.writeLog(String.Format("TMX path: {0}", pathTMX)); | |
int[] layerData = LoadTMX(pathTMX); | |
var formMain = formScript.getFormMain(); | |
int currentScreenNo = formMain.screenNo; | |
formScript.writeLog(String.Format("Current screen: {0} = 0x{0:X} (+1)", currentScreenNo)); | |
int layoutNo = ScreenNoToLayoutNo(currentScreenNo); | |
formScript.writeLog(String.Format("Current group/layout/level: {0}", layoutNo)); | |
var layout = ConfigScript.getLayout(layoutNo); | |
int width = formMain.screens[currentScreenNo].width; | |
int height = formMain.screens[currentScreenNo].height; | |
int layerWidth = layout.width * width; | |
int layerHeight = layout.height * height; | |
if (layerData.Length != layerWidth * layerHeight) | |
{ | |
formScript.writeLog("[Error] Invalid screen data size"); | |
return; | |
} | |
for (int sy = 0; sy < layout.height; sy++) | |
{ | |
for (int sx = 0; sx < layout.width; sx++) | |
{ | |
int scrIndex = sy * layout.width + sx; | |
int scrNo = CalcScrNo(layout, scrIndex); | |
if (scrNo >= 0 && scrNo < formMain.screens.Length) | |
{ | |
var curScreen = formMain.screens[scrNo]; | |
var curScreenData = curScreen.layers[0].data; | |
for (int y = 0; y < height; y++) | |
{ | |
for (int x = 0; x < width; x++) | |
{ | |
int index = y * width + x; | |
int lx = sx * width + x; | |
int ly = sy * height + y; | |
int tileNo = layerData[ly * layerWidth + lx]; | |
ConfigScript.setBigTileToScreen(curScreenData, index, tileNo); | |
} | |
} | |
} | |
} | |
} | |
formMain.setDirty(); | |
formScript.writeLog("########"); | |
} | |
public int[] LoadTMX(string path) | |
{ | |
return GetDataFromBase64Zip(GetBase64ZipFromTMX(path)); | |
} | |
public string GetBase64ZipFromTMX(string path) | |
{ | |
XDocument xdoc = XDocument.Load(path); | |
return xdoc.Element("map").Element("layer").Element("data").Value; | |
} | |
public int[] GetDataFromBase64Zip(string str) | |
{ | |
int[] layerData; | |
using (var decompressedStream = new MemoryStream()) | |
{ | |
using (var compStream = new MemoryStream(Convert.FromBase64String(str))) | |
using (var decompressionStream = new GZipStream(compStream, CompressionMode.Decompress)) | |
{ | |
decompressionStream.CopyTo(decompressedStream); | |
} | |
decompressedStream.Position = 0; | |
layerData = new int[decompressedStream.Length / sizeof(int)]; | |
using (var reader = new BinaryReader(decompressedStream)) { | |
for (int i = 0; i < layerData.Length; i++) { | |
layerData[i] = reader.ReadInt32() - 1; | |
} | |
} | |
} | |
return layerData; | |
} | |
private int CalcScrNo(LevelLayerData layout, int noInLayout) | |
{ | |
return layout.layer[noInLayout] - 1; | |
} | |
private int ScreenNoToLayoutNo(int scrNo) | |
{ | |
scrNo++; | |
var groups = ConfigScript.getGroups(); | |
int i = 0; | |
for (; i < groups.Length - 1; i++) | |
{ | |
if (scrNo >= groups[i].firstScreen && scrNo < groups[i + 1].firstScreen) | |
{ | |
return i; | |
} | |
} | |
return i; | |
} | |
private string OpenFileDialogTMX(string initialDir, string initialFileName) | |
{ | |
var filePath = string.Empty; | |
using (OpenFileDialog openFileDialog = new OpenFileDialog()) | |
{ | |
openFileDialog.InitialDirectory = initialDir; | |
openFileDialog.FileName = initialFileName; | |
openFileDialog.Filter = "TMX files (*.tmx)|*.tmx|All files (*.*)|*.*"; | |
openFileDialog.FilterIndex = 1; | |
if (openFileDialog.ShowDialog() == DialogResult.OK) | |
{ | |
return openFileDialog.FileName; | |
} | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment