diff --git a/App.xaml b/App.xaml index f598828..2733178 100644 --- a/App.xaml +++ b/App.xaml @@ -1,7 +1,7 @@ - diff --git a/App.xaml.cs b/App.xaml.cs index 4cd2db0..167d1c8 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -2,12 +2,12 @@ using System.Data; using System.Windows; -namespace WpfApp7 +namespace Migrator { /// /// Interaction logic for App.xaml /// - public partial class App : Application + public partial class App : System.Windows.Application { } diff --git a/H5MotaUpdate.csproj b/H5MotaUpdate.csproj index 2d044b9..b5d81a9 100644 --- a/H5MotaUpdate.csproj +++ b/H5MotaUpdate.csproj @@ -6,6 +6,7 @@ enable enable true + true diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 78b1f73..6a55a30 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -131,14 +131,26 @@ namespace H5MotaUpdate.ViewModels if (!CheckValid()) return; Version ver; Version.TryParse(VersionString, out ver); - int width = StringUtils.ReadMapWidth(Path.Combine(SourceRootDirectory, "libs/core.js")); + + #region + // 从libs/core.js中读取塔的默认长宽,若不为13,需要写入新样板的core.js中 + int width, height; + string sourceCoreJSPath = Path.Combine(SourceRootDirectory, "libs/core.js"), + destCoreJSPath = Path.Combine(DestRootDirectory, "libs/core.js"); + (width, height) = StringUtils.ReadMapWidth(sourceCoreJSPath); + + if (width != 13 || height != 13) + { + StringUtils.WriteMapWidth(destCoreJSPath, width, height); + } + #endregion DataJSMigrator dataJSMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver); EnemysJSMigrator enemysJSMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver); IconsJSMigrator iconsJSMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver); ItemsJSMigrator itemsJSMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver); MapsJSMigrator mapsJSMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver); - FloorsMigrator floorsMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver, width); + FloorsMigrator floorsMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver, width, height); MediaSourceMigrator mediaSourceJSMigrator = new(SourceProjectDirectory, DestProjectDirectroy, ver); dataJSMigrator.Migrate(); diff --git a/ViewModels/Migrator/FloorsMigrator.cs b/ViewModels/Migrator/FloorsMigrator.cs index 6e53c4c..4f075c7 100644 --- a/ViewModels/Migrator/FloorsMigrator.cs +++ b/ViewModels/Migrator/FloorsMigrator.cs @@ -27,17 +27,18 @@ namespace H5MotaUpdate.ViewModels Version version; readonly string FILENAME = "floors"; string?[] mapsIndexArray; - int mapWidth = 13; + int mapWidth, mapHeight; /// /// 请输入新旧Project文件夹的路径 /// - public FloorsMigrator(string oldProjectDirectory, string newProjectDirectory, Version ver, int width) + public FloorsMigrator(string oldProjectDirectory, string newProjectDirectory, Version ver, int width, int height) { sourcePath = System.IO.Path.Combine(oldProjectDirectory, FILENAME); destPath = System.IO.Path.Combine(newProjectDirectory, FILENAME); this.version = ver; this.mapWidth = width; + this.mapHeight = height; } public void Migrate(string?[] mapsIndexArray) @@ -104,10 +105,20 @@ namespace H5MotaUpdate.ViewModels jsonObject["canFlyFrom"] = jsonObject["canFlyTo"]; jsonObject["ratio"] = jsonObject["item_ratio"]; jsonObject.Remove("item_ratio"); - jsonObject["width"] = mapWidth; - jsonObject["height"] = mapWidth; jsonObject["autoEvent"] = new JObject(); + // 设置每张地图的尺寸。如果自己有就用自己的,否则设为默认长宽。 + JToken width = jsonObject["width"], + height = jsonObject["height"]; + if (width == null) + { + jsonObject["width"] = mapWidth; + } + if (height == null) + { + jsonObject["height"] = mapHeight; + } + #region // 楼层贴图:老版本为一字符串或数组,字符串会被自动转为[0,0,str] // 其中t[0],t[1]分别为x,y,t[2]为贴图名字 剩下的不知道干嘛的 diff --git a/ViewModels/Utils/StringUtils.cs b/ViewModels/Utils/StringUtils.cs index af388eb..384e86d 100644 --- a/ViewModels/Utils/StringUtils.cs +++ b/ViewModels/Utils/StringUtils.cs @@ -144,31 +144,84 @@ namespace H5MotaUpdate.ViewModels /// /// 读取塔的地图尺寸,默认值为13 /// - public static int ReadMapWidth(string filePath) + public static (int, int) ReadMapWidth(string filePath) { - int width; + /* + * 直到2.9为止,地图默认尺寸在libs/core.js中 this.__SIZE__ = 13 + * 老版本写法如下:this.bigmap = { + * width: 13, // map width and height + * height: 13, + * } + */ + int width, height; try { string fileContent = File.ReadAllText(filePath); - string widthPattern = @"this\._WIDTH_\s*=\s*(\d+);"; + // gpt写的,我也看不懂,就当它们是对的,有错再说 + string widthPattern = @"this\._WIDTH_\s*=\s*(\d+);", + heightPattern = @"this\._HEIGHT_\s*=\s*(\d+);", + sizePattern = @"this\.__SIZE__\s*=\s*(\d+);", + oldSizePattern = @"this\.bigmap\s*=\s*\{[^}]*width:\s*(\d+)[^}]*height:\s*(\d+)[^}]*\}"; - Match widthMatch = Regex.Match(fileContent, widthPattern); - - if (widthMatch.Success) + Match widthMatch = Regex.Match(fileContent, widthPattern), + heightMatch = Regex.Match(fileContent, heightPattern); + if (widthMatch.Success && heightMatch.Success) { width = int.Parse(widthMatch.Groups[1].Value); + height = int.Parse(heightMatch.Groups[1].Value); } else { - width = 13; + Match sizeMatch = Regex.Match(fileContent, sizePattern); + if (sizeMatch.Success) + { + width = int.Parse(sizeMatch.Groups[1].Value); + height = width; + } + else + { + Match oldWidthMatch = Regex.Match(fileContent, oldSizePattern); + if (oldWidthMatch.Success) + { + width = int.Parse(oldWidthMatch.Groups[1].Value); + height = int.Parse(oldWidthMatch.Groups[2].Value); + } + else + { + width = 13; + height = 13; + } + } } } catch { width = 13; + height = 13; + } + return (width, height); + } + + + /// + /// 将塔的地图尺寸写入libs/core.js + /// + public static void WriteMapWidth(string destFilePath, int width, int height) + { + try { + + string tempFilePath = destFilePath + ".tmp"; + string fileContent = File.ReadAllText(destFilePath); + fileContent = Regex.Replace(fileContent, @"this\._WIDTH_\s*=\s*\d+;", $"this._WIDTH_ = {width};"); + fileContent = Regex.Replace(fileContent, @"this\._HEIGHT_\s*=\s*\d+;", $"this._HEIGHT_ = {height};"); + File.WriteAllText(tempFilePath, fileContent); + File.Delete(destFilePath); + File.Move(tempFilePath, destFilePath); + } + catch { + } - return width; } } }