Stan0033

Class for Save/Load warcraft 3 models with MDXLIB

Oct 17th, 2025 (edited)
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1.  static class ModelSaverLoader
  2.  {
  3.      public static CModel? Load(string FromFileName)
  4.      {
  5.          CModel TemporaryModel = new CModel();
  6.          string extension = System.IO.Path.GetExtension(FromFileName).ToLower();
  7.          if (extension == ".mdx")
  8.          {
  9.              try
  10.              {
  11.                  using (var Stream = new System.IO.FileStream(FromFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
  12.                  {
  13.                      var ModelFormat = new MdxLib.ModelFormats.CMdx();
  14.                      ModelFormat.Load(FromFileName, Stream, TemporaryModel);
  15.                  }
  16.              }
  17.              catch (Exception ex)
  18.              {
  19.                  MessageBox.Show(ex.ToString(), $"Exception occured while trying to open {FromFileName}");
  20.                  // CurrentModel = new CModel();
  21.                  return null;
  22.              }
  23.          }
  24.          if (extension == ".mdl")
  25.          {
  26.              try
  27.              {
  28.                  using (var Stream = new System.IO.FileStream(FromFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
  29.                  {
  30.                      var ModelFormat = new MdxLib.ModelFormats.CMdl();
  31.                      ModelFormat.Load(FromFileName, Stream, TemporaryModel);
  32.                  }
  33.              }
  34.              catch (Exception ex)
  35.              {
  36.                  MessageBox.Show(ex.ToString(), $"Exception occured while trying to open {FromFileName}");
  37.                  //  CurrentModel = new CModel();
  38.                  return null;
  39.              }
  40.          }
  41.          return TemporaryModel;
  42.      }
  43.      internal static void Save(CModel model, string file)
  44.      {
  45.          if (file.Length == 0) { MessageBox.Show("Empty save path"); return; }
  46.          if (
  47.             (System.IO.Path.GetExtension(file) == ".mdx" ||
  48.              System.IO.Path.GetExtension(file) == ".mdl") == false
  49.              ) { MessageBox.Show("Invalid extension"); return; }
  50.          if (model == null) { MessageBox.Show("Null model"); return; }
  51.          if (System.IO.Path.GetExtension(file).ToLower() == ".mdl")
  52.          {
  53.              string ToFileName = file;
  54.              using (var Stream = new System.IO.FileStream(ToFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
  55.              {
  56.                  var ModelFormat = new MdxLib.ModelFormats.CMdl();
  57.                  ModelFormat.Save(ToFileName, Stream, model);
  58.              }
  59.              FileCleaner.CleanFile(ToFileName);
  60.          }
  61.          if (System.IO.Path.GetExtension(file).ToLower() == ".mdx")
  62.          {
  63.              string ToFileName = file;
  64.              using (var Stream = new System.IO.FileStream(ToFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
  65.              {
  66.                  var ModelFormat = new MdxLib.ModelFormats.CMdx();
  67.                  ModelFormat.Save(ToFileName, Stream, model);
  68.              }
  69.          }
  70.      }
  71.  }
  72. public class FileCleaner
  73. {
  74.     public static void CleanFile(string filePath)
  75.     {
  76.         // Read the file content
  77.         string content = File.ReadAllText(filePath);
  78.         // Define the valid character set
  79.         StringBuilder cleanedContent = new StringBuilder();
  80.         foreach (char c in content)
  81.         {
  82.             if (IsValidCharacter(c))
  83.             {
  84.                 cleanedContent.Append(c);
  85.             }
  86.         }
  87.         // Delete the original file
  88.         File.Delete(filePath);
  89.         // Write the cleaned content to a new file
  90.         File.WriteAllText(filePath, cleanedContent.ToString());
  91.     }
  92.     private static bool IsValidCharacter(char c)
  93.     {
  94.         // Check if the character is a valid English letter, symbol, digit, tab, or newline
  95.         return char.IsLetterOrDigit(c) ||
  96.                char.IsWhiteSpace(c) ||  // This includes tabs, spaces, and all other whitespaces
  97.                c == '\n' || c == '\r' || // Newline and Carriage return
  98.                IsValidSymbol(c);
  99.     }
  100.     private static bool IsValidSymbol(char c)
  101.     {
  102.         // Define the valid symbols (you can extend this list if needed)
  103.         char[] validSymbols = new char[]
  104.         {
  105.             '!', '"', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/',
  106.             ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{',
  107.             '|', '}', '~'
  108.         };
  109.         foreach (char symbol in validSymbols)
  110.         {
  111.             if (c == symbol)
  112.             {
  113.                 return true;
  114.             }
  115.         }
  116.         return false;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment