Skip to content

Commit

Permalink
Merge pull request #6 from AtomCrafty/feature-auto
Browse files Browse the repository at this point in the history
Feature: auto
  • Loading branch information
AtomCrafty authored Jan 28, 2017
2 parents 18faf8c + c1a9726 commit 4c86930
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/yukatool/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>
</configuration>
3 changes: 3 additions & 0 deletions src/yukatool/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Constants {
public const string unwrappedGraphicsExtension = "png";
public const string frameMetaExtension = "meta";

public const string archiveExtension = "ykc";
public const string patchLogExtension = "ypl";

public static Dictionary<string, string> knownVars = new Dictionary<string, string>();

static Constants() {
Expand Down
12 changes: 12 additions & 0 deletions src/yukatool/Data/DataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Yuka.Data {
[Flags]
enum DataType {
None = 0,
Archive = 1,
Graphics = 2,
Script = 4,
Raw = 8
}
}
5 changes: 3 additions & 2 deletions src/yukatool/Data/Factory/ArchiveFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override YukaArchive FromBinary(Stream s) {
uint datalength = br.ReadUInt32();

s.Seek(nameoffset, SeekOrigin.Begin);
string name = Encoding.ASCII.GetString(br.ReadBytes((int)namelength - 1));
string name = Encoding.ASCII.GetString(br.ReadBytes((int)namelength - 1)).ToLower();
s.Seek(dataoffset, SeekOrigin.Begin);
byte[] data = br.ReadBytes((int)datalength);

Expand All @@ -50,9 +50,10 @@ public override long ToBinary(YukaArchive data, Stream s) {
// Write data sector
foreach(var file in data.files) {
uint dataoffset = (uint)s.Position;
/*
if(FlagCollection.current.Has('v')) {
Console.WriteLine("Packing file: " + file.Key);
}
}//*/
MemoryStream ms = data.GetInputStream(file.Key);
ms.CopyTo(s);
//s.Write(data, 0, data.Length);
Expand Down
4 changes: 2 additions & 2 deletions src/yukatool/Data/YukaArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Yuka.Data {
class YukaArchive : YukaFile {
public Dictionary<string, MemoryStream> files;

public YukaArchive() {
public YukaArchive() : base(DataType.Archive) {
files = new Dictionary<string, MemoryStream>();
}

public YukaArchive(Dictionary<string, MemoryStream> files) {
public YukaArchive(Dictionary<string, MemoryStream> files) : base(DataType.Archive) {
this.files = files;
}

Expand Down
4 changes: 4 additions & 0 deletions src/yukatool/Data/YukaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@

namespace Yuka.Data {
class YukaFile {
public DataType Type { get; }
protected YukaFile(DataType type) {
Type = type;
}
}
}
4 changes: 2 additions & 2 deletions src/yukatool/Data/YukaGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class YukaGraphics : YukaFile {
public Bitmap bitmap;
public byte[] metaData;

public YukaGraphics(Bitmap bitmap) {
public YukaGraphics(Bitmap bitmap) : base(DataType.Graphics) {
this.bitmap = bitmap;
}

public YukaGraphics(Bitmap bitmap, byte[] metaData) {
public YukaGraphics(Bitmap bitmap, byte[] metaData) : base(DataType.Graphics) {
this.bitmap = bitmap;
this.metaData = metaData;
}
Expand Down
2 changes: 1 addition & 1 deletion src/yukatool/Data/YukaRaw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Yuka.Data {
class YukaRaw : YukaFile {
public byte[] data;

public YukaRaw(byte[] data) {
public YukaRaw(byte[] data) : base(DataType.Raw) {
this.data = data;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/yukatool/Data/YukaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class YukaScript : YukaFile {
public List<ScriptElement> commands;
public Dictionary<string, string> stringTable;

public YukaScript(List<ScriptElement> commands, Dictionary<string, string> stringTable) {
public YukaScript(List<ScriptElement> commands, Dictionary<string, string> stringTable) : base(DataType.Script) {
this.commands = commands;
this.stringTable = stringTable;
}
Expand Down
Binary file added src/yukatool/Newtonsoft.Json.dll
Binary file not shown.
26 changes: 23 additions & 3 deletions src/yukatool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.IO;
using System.Reflection;
using Yuka.Tasks;

namespace Yuka {
class Program {

static void Setup() {
// Register tasks
Task.Register("help", new HelpTask());
Expand All @@ -15,15 +18,17 @@ static void Setup() {
Task.Register("unwrap", new UnwrapTask());
Task.Register("wrap", new WrapTask());

Task.SetDefault("help");
Task.Register("auto", new AutoTask());

Task.SetDefault("auto");
}

static void Run(string[] args) {
#if !DEBUG
try {
#endif
Task task = Task.Create(args);
task.Run();
Task task = Task.Create(args);
task.Run();
#if !DEBUG
}
catch(Exception e) {
Expand All @@ -39,5 +44,20 @@ static void Main(string[] args) {
Setup();
Run(args);
}

static Program() {
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Resolver);
}

static Assembly Resolver(object sender, ResolveEventArgs args) {
string resourceName = "Yuka." + args.Name.Split(',')[0] + ".dll";

Assembly ea = Assembly.GetExecutingAssembly();
Stream s = ea.GetManifestResourceStream(resourceName);
byte[] block = new byte[s.Length];
s.Read(block, 0, block.Length);
Assembly la = Assembly.Load(block);
return la;
}
}
}
27 changes: 25 additions & 2 deletions src/yukatool/Task.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Yuka {
abstract class Task {
Expand All @@ -17,6 +18,14 @@ abstract class Task {
public FlagCollection flags;
public string[] arguments;

public Task() {
}

public Task(string[] arguments, FlagCollection flags) {
this.arguments = arguments;
this.flags = flags;
}

public void Run() {
FlagCollection parentFlags = FlagCollection.current;
FlagCollection.current = flags;
Expand All @@ -27,12 +36,26 @@ public void Run() {
FlagCollection.current = parentFlags;
}

[DebuggerStepThrough]
public void Fail(string message) {
throw new Exception(message);
}

[DebuggerStepThrough]
public void Log(string message) {
Console.WriteLine(message);
if(flags.Has('v')) {
Console.WriteLine(message);
}
}

[DebuggerStepThrough]
public void Log(string message, ConsoleColor color) {
if(flags.Has('v')) {
ConsoleColor old = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ForegroundColor = old;
}
}

public static Task Create(string[] args) {
Expand Down Expand Up @@ -84,7 +107,7 @@ public static Task Create(string[] args) {
task = task.NewTask();
task.flags = flags;
task.arguments = param.ToArray();

return task;
}

Expand Down
74 changes: 74 additions & 0 deletions src/yukatool/Tasks/AutoTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Yuka.Data;
using Yuka.Data.Factory;
using Yuka.Script;

namespace Yuka.Tasks {
class AutoTask : Task {
public override Task NewTask() {
return new AutoTask();
}

public override void DefaultFlags(FlagCollection flags) {
flags.Add('v', "verbose", "Outputs additional information", false);
flags.Add('w', "wait", "Waits for enter before closing the console", false);
}

protected override void Execute() {
if(arguments.Length == 0) {
Task task = new HelpTask();
task.arguments = new string[0];
task.flags = flags;
task.Run();
}
else if(arguments[0].EndsWith(".patch.ykc")) {
// patch mode
Task task = null;
for(int i = 1; i <= 5; i++) {
if(Path.GetFileName(arguments[0]).Contains(".data0" + i + '.')) {
if(File.Exists(Path.Combine(Path.GetDirectoryName(arguments[0]), "data0" + i + ".ykc"))) {
flags.Set('v');
task = new PatchTask(new[] { arguments[0], Path.Combine(Path.GetDirectoryName(arguments[0]), "data0" + i + ".ykc") }, flags);
}
else if(File.Exists(Helpers.AbsolutePath("data0" + i + ".ykc"))) {
flags.Set('v');
task = new PatchTask(new[] { arguments[0], Path.Combine(Directory.GetCurrentDirectory(), "data0" + i + ".ykc") }, flags);
}
break;
}
}
if(task == null) {
Fail("Unable to determine patch target");
}
else {
task.Run();
}
Console.ReadLine();
}
else if(Path.GetExtension(arguments[0]) == ".ykg") {
string path = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetFileName(arguments[0]), "png"));
using(FileStream fs = new FileStream(Helpers.AbsolutePath(arguments[0]), FileMode.Open)) {
YukaGraphics g = GraphicsFactory.Instance.FromBinary(fs);
g.bitmap.Save(path);
System.Diagnostics.Process.Start(path);
}
}
else if(Directory.Exists(arguments[0])) {
string patchLogPath = Path.Combine(Helpers.AbsolutePath(arguments[0]), "patch.ypl");
if(File.Exists(patchLogPath)) {
}
}
else if(Path.GetExtension(arguments[0]) == ".ypl") {
flags.Set('v');
flags.Set('w');
new PatchTask(arguments, flags).Run();
}
else {
Fail("Unable to auto determine processing method");
}
}
}
}
3 changes: 3 additions & 0 deletions src/yukatool/Tasks/PackTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public override Task NewTask() {
return new PackTask();
}

public PackTask() { }
public PackTask(string[] arguments, FlagCollection flags) : base(arguments, flags) { }

public override void DefaultFlags(FlagCollection flags) {
flags.Add('v', "verbose", "Outputs additional information", false);
flags.Add('w', "wait", "Waits for enter before closing the console", false);
Expand Down
Loading

0 comments on commit 4c86930

Please sign in to comment.