Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class ModelSaverLoader
- {
- public static CModel? Load(string FromFileName)
- {
- CModel TemporaryModel = new CModel();
- string extension = System.IO.Path.GetExtension(FromFileName).ToLower();
- if (extension == ".mdx")
- {
- try
- {
- using (var Stream = new System.IO.FileStream(FromFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
- {
- var ModelFormat = new MdxLib.ModelFormats.CMdx();
- ModelFormat.Load(FromFileName, Stream, TemporaryModel);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString(), $"Exception occured while trying to open {FromFileName}");
- // CurrentModel = new CModel();
- return null;
- }
- }
- if (extension == ".mdl")
- {
- try
- {
- using (var Stream = new System.IO.FileStream(FromFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
- {
- var ModelFormat = new MdxLib.ModelFormats.CMdl();
- ModelFormat.Load(FromFileName, Stream, TemporaryModel);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString(), $"Exception occured while trying to open {FromFileName}");
- // CurrentModel = new CModel();
- return null;
- }
- }
- return TemporaryModel;
- }
- internal static void Save(CModel model, string file)
- {
- if (file.Length == 0) { MessageBox.Show("Empty save path"); return; }
- if (
- (System.IO.Path.GetExtension(file) == ".mdx" ||
- System.IO.Path.GetExtension(file) == ".mdl") == false
- ) { MessageBox.Show("Invalid extension"); return; }
- if (model == null) { MessageBox.Show("Null model"); return; }
- if (System.IO.Path.GetExtension(file).ToLower() == ".mdl")
- {
- string ToFileName = file;
- using (var Stream = new System.IO.FileStream(ToFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
- {
- var ModelFormat = new MdxLib.ModelFormats.CMdl();
- ModelFormat.Save(ToFileName, Stream, model);
- }
- FileCleaner.CleanFile(ToFileName);
- }
- if (System.IO.Path.GetExtension(file).ToLower() == ".mdx")
- {
- string ToFileName = file;
- using (var Stream = new System.IO.FileStream(ToFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
- {
- var ModelFormat = new MdxLib.ModelFormats.CMdx();
- ModelFormat.Save(ToFileName, Stream, model);
- }
- }
- }
- }
- public class FileCleaner
- {
- public static void CleanFile(string filePath)
- {
- // Read the file content
- string content = File.ReadAllText(filePath);
- // Define the valid character set
- StringBuilder cleanedContent = new StringBuilder();
- foreach (char c in content)
- {
- if (IsValidCharacter(c))
- {
- cleanedContent.Append(c);
- }
- }
- // Delete the original file
- File.Delete(filePath);
- // Write the cleaned content to a new file
- File.WriteAllText(filePath, cleanedContent.ToString());
- }
- private static bool IsValidCharacter(char c)
- {
- // Check if the character is a valid English letter, symbol, digit, tab, or newline
- return char.IsLetterOrDigit(c) ||
- char.IsWhiteSpace(c) || // This includes tabs, spaces, and all other whitespaces
- c == '\n' || c == '\r' || // Newline and Carriage return
- IsValidSymbol(c);
- }
- private static bool IsValidSymbol(char c)
- {
- // Define the valid symbols (you can extend this list if needed)
- char[] validSymbols = new char[]
- {
- '!', '"', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/',
- ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{',
- '|', '}', '~'
- };
- foreach (char symbol in validSymbols)
- {
- if (c == symbol)
- {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment