Skip to content

Commit

Permalink
Merge pull request #1 from CreativeMD/master
Browse files Browse the repository at this point in the history
Added project solution
  • Loading branch information
CreativeMD authored May 24, 2018
2 parents a10a585 + a0393e4 commit 5dedb56
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
obj
.vs
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# vsmodtools
# vsmodtools

A simple program which can run under Windows, Linux and MacOS. It can add and pack mods in your VintageStory Mod Solution.
25 changes: 25 additions & 0 deletions vsmodtools.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2000
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "vsmodtools", "vsmodtools\vsmodtools.csproj", "{B9506947-1C30-42E0-A663-1B38AE3442AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B9506947-1C30-42E0-A663-1B38AE3442AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9506947-1C30-42E0-A663-1B38AE3442AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9506947-1C30-42E0-A663-1B38AE3442AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9506947-1C30-42E0-A663-1B38AE3442AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BAEEF8B0-2871-46CD-919F-00A712C6A66E}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions vsmodtools/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
162 changes: 162 additions & 0 deletions vsmodtools/Creator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace vsmodtools
{
public class Creator
{

public static void Init()
{
Program.registerCommand(new AddModCommand());
}

public static bool IsValidModID(string str)
{
if (string.IsNullOrEmpty(str)) return false;
for (var i = 0; i < str.Length; i++)
{
var chr = str[i];
var isLetter = (chr >= 'a') && (chr <= 'z');
var isDigit = (chr >= '0') && (chr <= '9');
if (isLetter || (isDigit && (i != 0))) continue;
return false;
}
return true;
}

public static IEnumerable<string> ReadLines(string filename, Dictionary<string, string> variables)
{
return ReadLines(() => Assembly.GetExecutingAssembly().GetManifestResourceStream(filename), Encoding.UTF8, variables);
}

public static IEnumerable<string> ReadLines(Func<Stream> streamProvider, Encoding encoding, Dictionary<string, string> variables)
{
using (var stream = streamProvider())
using (var reader = new StreamReader(stream, encoding))
{
string line;
while ((line = reader.ReadLine()) != null)
{
foreach (var variable in variables)
line = line.Replace(variable.Key, variable.Value);
yield return line;
}
}
}

}

public class AddModCommand : Command
{

public AddModCommand() : base("add", "/add <modid>", "Adds a new mod to the solution")
{

}

public override bool Run(string[] args)
{
if(args.Length <= 1)
{
Console.WriteLine("Missing modid!");
return false;
}

string modid = args[1];
if (Creator.IsValidModID(modid))
{
Console.WriteLine("'{0}' appears to be a valid modid. Creating new mod ...", modid);
Assembly assembly = Assembly.GetExecutingAssembly();
string folder = Path.GetDirectoryName(assembly.Location) + Path.DirectorySeparatorChar + "mods" + Path.DirectorySeparatorChar + modid + Path.DirectorySeparatorChar;
if (Directory.CreateDirectory(folder).Exists)
{
Dictionary<string, string> variables = new Dictionary<string, string>();
variables.Add("$(modid)", modid);
variables.Add("$(gameversion)", "1.5.3");

string projectfile = folder + modid + ".csproj";
if (!File.Exists(projectfile))
{
File.WriteAllLines(projectfile, Creator.ReadLines("vsmodtools.project.template", variables));
File.WriteAllLines(folder + "modinfo.json", Creator.ReadLines("vsmodtools.modinfo.template", variables));
Directory.CreateDirectory(folder + "src");
Directory.CreateDirectory(folder + "assets");
Console.WriteLine("Created " + modid + " successfully ...");

string solutionfile = Path.GetDirectoryName(assembly.Location) + Path.DirectorySeparatorChar + "VSMods.sln";
if (File.Exists(solutionfile))
{
string projectID = "{" + Guid.NewGuid().ToString() + "}";
List<string> list = File.ReadLines(solutionfile).ToList();
int step = 0;
for (int i = 0; i < list.Count; i++)
{
var line = list[i];
switch (step)
{
case 0:
if (line.Contains("\"VSModLauncher.csproj\""))
step = 1;
break;
case 1:
if (line.Replace(" ", "").Equals("EndProject", StringComparison.OrdinalIgnoreCase))
{
i++;
list.Insert(i, "EndProject");
list.Insert(i, "Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"" + modid + "\", \"mods" + Path.DirectorySeparatorChar + modid + Path.DirectorySeparatorChar + modid + ".csproj\", \"" + projectID + "\"");
i++;
step = 2;
}
break;
case 2:
if (line.Replace(" ", "").Contains("GlobalSection(ProjectConfigurationPlatforms)=postSolution"))
step = 3;
break;
case 3:
if (line.Contains("EndGlobalSection"))
{

list.InsertRange(i, new string[] { projectID + ".Debug|Any CPU.ActiveCfg = Debug|Any CPU",
projectID + ".Debug|Any CPU.Build.0 = Debug|Any CPU",
projectID + ".Release x64|Any CPU.ActiveCfg = Release|Any CPU",
projectID + ".Release x64|Any CPU.Build.0 = Release|Any CPU",
projectID + ".Release|Any CPU.ActiveCfg = Release|Any CPU",
projectID + ".Release|Any CPU.Build.0 = Release|Any CPU}" });
step = 4;
i = list.Count;
}
break;
}
}
if (step < 4)
Console.WriteLine("Something went wrong, could not complete solution injection (" + step + ")");
else
{
File.WriteAllLines(solutionfile, list);
Console.WriteLine("Solution injection complete ...");
return true;
}

}
else
Console.WriteLine("Could not find solution!");
}
else
Console.WriteLine("This mod exists already!");
}

}
else
Console.WriteLine("'{0} is not a valid modid!", modid);

return false;
}

}
}
155 changes: 155 additions & 0 deletions vsmodtools/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vsmodtools
{
class Program
{
internal static List<Command> commands = new List<Command>();
internal static Command currentCommand;

public const string version = "1.0";

static void Init()
{
// Register commands
registerCommand(new HelpCommand());
registerCommand(new ExitCommand());

Creator.Init();

commands = commands.OrderBy(x => x).ToList();

Console.WriteLine("VintageStory ModTools v{0}", version);
}

public static void registerCommand(Command command)
{
commands.Add(command);
}

public static string GetCommandName(string[] args)
{
if (args == null || args.Length == 0)
return "";
return args[0];
}

public static Command GetCommand(string[] args)
{
if (args.Length == 0)
return null;

string name = args[0];
foreach(var command in commands)
if (command.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
return command;

if(!String.IsNullOrEmpty(name))
Console.WriteLine("Command '{0}' could not be found!", name);

return null;
}

static void Main(string[] args)
{
Init();

start:
if(args == null || args.Length == 0)
{
Console.Write("> ");
string text = Console.ReadLine();
args = Program.ParseArguments(text);
}

currentCommand = GetCommand(args);
string name = GetCommandName(args);
if (currentCommand == null || !currentCommand.Run(args))
{
args = null;
goto start;
}

Console.WriteLine("\nPress any key to continue ...");
Console.ReadKey();
}

public static string[] ParseArguments(string commandLine)
{
char[] parmChars = commandLine.ToCharArray();
bool inQuote = false;
for (int index = 0; index < parmChars.Length; index++)
{
if (parmChars[index] == '"')
inQuote = !inQuote;
if (!inQuote && parmChars[index] == ' ')
parmChars[index] = '\n';
}
return (new string(parmChars)).Split('\n');
}
}

public abstract class Command : IComparable
{
public readonly string Name;
public readonly string Syntax;
public readonly string Description;

public Command(string name, string syntax, string description)
{
this.Name = name;
this.Syntax = syntax;
this.Description = description;
}

public Command(string name, string description) : this(name, "/" + name, description)
{

}

public int CompareTo(object obj)
{
if (obj is Command)
return Name.CompareTo((obj as Command).Name);
return Name.CompareTo(obj);
}

public abstract bool Run(string[] args);

}

class ExitCommand : Command
{
public ExitCommand() : base("exit", "Terminates the application")
{

}

public override bool Run(string[] args)
{
return true;
}
}

class HelpCommand : Command
{

public HelpCommand() : base("help", "Lists all available commands")
{

}

public override bool Run(string[] args)
{
Console.WriteLine("List of all commands:");
foreach(var command in Program.commands)
Console.WriteLine(command.Syntax + " - " + command.Description);
return false;
}
}

}
Loading

0 comments on commit 5dedb56

Please sign in to comment.