Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importer #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Scripts/Menu.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

296 changes: 296 additions & 0 deletions Assets/Scripts/Menu/FileImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace LOMN.Menu
{
public class FileImporter : EditorWindow
{
public enum VerisonType { None, Vanilla, Alpha, Beta, Rebuilt };
private enum PathFieldType { Folder, File };

VerisonType version;
string sourcePath;
bool deleteOldFiles;

string blkFilePath;
string xFilePath;

const string charactersFolder = "characters";
const string levelsFolder = "levels";

readonly string[] exceptionFilesForDelete = new string[] { "placeholder.txt", "placeholder.txt.meta" };

void OnEnable()
{
blkFilePath = Path.Combine(Application.dataPath, "..", "Outresources", "BlkConverter", "blkfile.py");
xFilePath = Path.Combine(Application.dataPath, "..", "Outresources", "XFileConverter", "LOMNTool.exe");
}

void OnGUI()
{
GUILayout.Label("File Import", EditorStyles.boldLabel);

version= (VerisonType)EditorGUILayout.EnumPopup("Version:", version);

GUILayout.BeginHorizontal(GUILayout.Height(30));
sourcePath = EditorGUILayout.TextField(" Source Path", sourcePath, GUILayout.MaxWidth(400));
if (GUILayout.Button("Browse", GUILayout.MaxWidth(75)))
sourcePath = EditorUtility.OpenFolderPanel("Add Source Path", "", "");
GUILayout.EndHorizontal();

deleteOldFiles = EditorGUILayout.Toggle("Delete Old Files", deleteOldFiles);

if (GUILayout.Button("Import"))
Import();
}

void Import()
{
if(version == VerisonType.None)
{
Debug.LogError("Version can't be None.");
return;
}

if (!IsFileReady("Blk File Converter", blkFilePath) || !IsFileReady("X File Converter", xFilePath) ||
!IsVersionEmpty() || !IsGamePathReady())
return;

ImportFiles();
ConvertBlkFiles();
ConvertXFiles();

Debug.Log(version.ToString() + " is ready");
}

void ImportFiles()
{
string baseTargetPath = Path.Combine(Application.dataPath, "Resources", version.ToString().ToLower());
string sourceCharacterPath = Path.Combine(sourcePath, charactersFolder);
string sourceLevelsPath = Path.Combine(sourcePath, levelsFolder);

List<string> files = GetFiles(sourceCharacterPath);
files.AddRange(GetFiles(sourceLevelsPath));
var progressBar = new ProgressBar(files.Count, version + "'s files import", "Start");

CopyFolder(sourceCharacterPath, Path.Combine(baseTargetPath, charactersFolder), progressBar);
CopyFolder(sourceLevelsPath, Path.Combine(baseTargetPath, levelsFolder), progressBar);

progressBar.Hide();
Debug.Log(version + "'s files is imported");
}

void CopyFolder(string sourcePath, string targetPath, ProgressBar progressBar)
{
Directory.CreateDirectory(targetPath);
string[] files = Directory.GetFiles(sourcePath);
foreach (string file in files)
{
progressBar.Info = file;
progressBar.Next();
progressBar.Refresh();
File.Copy(file, Path.Combine(targetPath, Path.GetFileName(file)), true);
}

string[] folders = Directory.GetDirectories(sourcePath);
foreach (string folder in folders)
{
string folderName = folder.Substring(sourcePath.Length + 1);
CopyFolder(folder, Path.Combine(targetPath, folderName), progressBar);
}
}

void ConvertBlkFiles()
{
string baseTargetPath = Path.Combine(Application.dataPath, "Resources", version.ToString().ToLower());

List<string> files = GetFiles(Path.Combine(baseTargetPath, charactersFolder),"*.blk");
files.AddRange(GetFiles(Path.Combine(baseTargetPath, levelsFolder), "*.blk"));

var progressBar = new ProgressBar(files.Count, version + "'s blk files convert.", "Start");

var process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";

foreach(string file in files)
{
progressBar.Info = file;
progressBar.Next();
progressBar.Refresh();
startInfo.Arguments = "/C "+ blkFilePath + " -c "+ file;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

File.Delete(file);
}

progressBar.Hide();
Debug.Log(version + "'s blk files is converted.");
}

void ConvertXFiles()
{
string baseTargetPath = Path.Combine(Application.dataPath, "Resources", version.ToString().ToLower());

List<string> files = GetFiles(Path.Combine(baseTargetPath, charactersFolder), "*.x");
files.AddRange(GetFiles(Path.Combine(baseTargetPath, levelsFolder), "*.x"));

var progressBar = new ProgressBar(files.Count, version + "'s x files concert.", "Start");

var process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";

foreach (string file in files)
{
progressBar.Info = file;
progressBar.Next();
progressBar.Refresh();
startInfo.Arguments = "/C " + xFilePath + " " + file;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

File.Delete(file);
}

progressBar.Hide();
Debug.Log(version + "'s x files is converted.");
}

bool IsVersionEmpty()
{
string baseTargetPath = Path.Combine(Application.dataPath, "Resources", version.ToString().ToLower());
string targetCharacterPath = Path.Combine(baseTargetPath, charactersFolder);
string targetLevelsPath = Path.Combine(baseTargetPath, levelsFolder);


var folders = new List<string>(Directory.GetDirectories(targetCharacterPath));
folders.AddRange(Directory.GetDirectories(targetLevelsPath));

if(!deleteOldFiles && folders.Count > 0)
{
Debug.LogError(version + " has folders.");
return false;
}


var files = new List<string>(Directory.GetFiles(targetCharacterPath));
files.AddRange(Directory.GetFiles(targetLevelsPath));
foreach (string file in exceptionFilesForDelete)
{
files.Remove(Path.Combine(targetCharacterPath, file));
files.Remove(Path.Combine(targetLevelsPath, file));
}

if(!deleteOldFiles && files.Count > 0)
{
Debug.LogError(version + " has files.");
return false;
}


if (deleteOldFiles)
{
var progressBar = new ProgressBar(folders.Count + files.Count, version + "'s files delete", "Start");

foreach (var folder in folders)
{
progressBar.Info = folder;
progressBar.Next();
progressBar.Refresh();
Directory.Delete(folder, true);
}

foreach (var file in files)
{
progressBar.Info = file;
progressBar.Next();
progressBar.Refresh();
File.Delete(file);
}

progressBar.Hide();
Debug.Log(version + "'s files is deleted");
}

return true;
}

bool IsGamePathReady()
{
if (String.IsNullOrEmpty(sourcePath))
{
Debug.LogWarning(version + " path is empty, that's why it will be skiped.");
return false;
}

if (!Directory.Exists(sourcePath))
{
Debug.LogError(version + " directory doesn't exist.");
return false;
}

string[] folders = Directory.GetDirectories(sourcePath);
bool foundCharacters = false;
bool foundLevels = false;

foreach (string folderPath in folders)
{
string folder = folderPath.Substring(sourcePath.Length + 1);
if (folder == charactersFolder)
foundCharacters = true;

if (folder == levelsFolder)
foundLevels = true;

if (foundCharacters && foundLevels)
break;
}


if (!foundCharacters || !foundLevels)
{
Debug.LogError(version + " directory hasn't characters and levels folders.");
return false;
}

return true;
}

bool IsFileReady(string name, string path)
{
if (String.IsNullOrEmpty(path))
{
Debug.LogError(name + " path is empty.");
return false;
}

if (!File.Exists(path))
{
Debug.LogError(name + " file doesn't exist.");
return false;
}

return true;
}

List<string> GetFiles(string path, string searchPattern="*")
{
var list = new List<string>(Directory.GetFiles(path, searchPattern));

string[] folders = Directory.GetDirectories(path);
foreach (string folder in folders)
list.AddRange(GetFiles(folder, searchPattern));

return list;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Menu/FileImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Scripts/Menu/LOMNMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEditor;
using UnityEngine;

namespace LOMN.Menu
{
public class LOMNMenu : MonoBehaviour
{
[MenuItem("LOMN/Import")]
static void OpenImport()
{
EditorWindow.GetWindow(typeof(FileImporter),false, "LOMN Import");
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Menu/LOMNMenu.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Assets/Scripts/Menu/ProgressBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using UnityEditor;

namespace LOMN.Menu
{
public class ProgressBar
{
public string Title;
public string Info;
public float CurrentProgress { get; private set; }
public readonly float FullProgress;

public ProgressBar(int fullProgress, string title = "", string info = "")
{
Title = title;
Info = info;
FullProgress = fullProgress;
}

public void Show()
{
EditorUtility.DisplayProgressBar(Title, Info, 0);
}

public void Hide()
{
EditorUtility.ClearProgressBar();
}

public void Refresh()
{
EditorUtility.DisplayProgressBar(Title+" "+ CurrentProgress + "/"+ FullProgress, Info, CurrentProgress / FullProgress);
}

public void Next(bool useRefresh = true)
{
++CurrentProgress;
}

public void Reset()
{
CurrentProgress = 0;
}

}
}
Loading