-
Notifications
You must be signed in to change notification settings - Fork 1
/
EffectProcessor.cs
199 lines (177 loc) · 6.42 KB
/
EffectProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework.Content;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using Newtonsoft.Json;
namespace Peon501.Pipeline.Processors
{
[ContentProcessor (DisplayName = "Effect Processor - Matas Lešinskas")]
public sealed class LocalEffectProcessor : EffectProcessor
{
[DefaultValue ("/.wine/drive_c/Program Files (x86)/MSBuild/MonoGame/v3.0/Tools/2MGFX.exe")]
public string CompilerPath { get; set; }
[DefaultValue ("/opt/wine-staging/bin/wine64")]
public string WinePath { get; set; }
public LocalEffectProcessor ()
{
CompilerPath = "/.wine/drive_c/Program Files (x86)/MSBuild/MonoGame/v3.0/Tools/2MGFX.exe";
WinePath = "/opt/wine-staging/bin/wine64";
}
void ReadInclude (StringBuilder sb, string filename)
{
foreach (var line in File.ReadAllLines (filename)) {
if (line.StartsWith ("//"))
continue;
if (line.StartsWith ("#include", StringComparison.InvariantCultureIgnoreCase)) {
var root = Path.GetDirectoryName (filename);
var startIndex = line.IndexOf ("\"") + 1;
var file = line.Substring (startIndex, line.IndexOf ("\"", startIndex) - startIndex);
ReadInclude (sb, Path.Combine (root, file));
}
else {
sb.AppendLine (line.Trim ());
}
}
}
string ResolveCode (EffectContent input)
{
StringBuilder sb = new StringBuilder ();
foreach (var line in input.EffectCode.Split (new char [] { '\n' })) {
if (line.StartsWith ("//", StringComparison.InvariantCultureIgnoreCase))
continue;
if (line.StartsWith ("#include", StringComparison.InvariantCultureIgnoreCase)) {
// read the file
var startIndex = line.IndexOf ("\"", StringComparison.InvariantCultureIgnoreCase) + 1;
var file = line.Substring (startIndex, line.IndexOf ("\"", startIndex, StringComparison.InvariantCultureIgnoreCase) - startIndex);
var root = Path.GetDirectoryName (input.Identity.SourceFilename);
ReadInclude (sb, Path.Combine (root, file));
}
else {
sb.AppendLine (line.Trim ());
}
}
return sb.ToString ();
}
public override CompiledEffectContent Process (EffectContent input, ContentProcessorContext context)
{
if (Environment.OSVersion.Platform != PlatformID.Unix) {
return base.Process (input, context);
}
var code = ResolveCode (input);
var platform = context.TargetPlatform;
var version = typeof (EffectContent).Assembly.GetName ().Version;
string error = String.Empty;
byte[] buf = Mgfx.RunMGCB(code, platform.ToString(), WinePath, CompilerPath, out error);
var resultSer = JsonConvert.SerializeObject (new Result() { Compiled = buf, Error = error });
var result = JsonDeSerializer (resultSer);
if (!string.IsNullOrEmpty (result.Error)) {
throw new Exception (result.Error);
}
if (result.Compiled == null || result.Compiled.Length == 0)
throw new Exception ("There was an error compiling the effect");
return new CompiledEffectContent (result.Compiled);
return null;
}
public string JsonSerializer(Data objectToSerialize)
{
return Newtonsoft.Json.JsonConvert.SerializeObject (objectToSerialize, Newtonsoft.Json.Formatting.None);
}
public Result JsonDeSerializer(string data)
{
return (Result)Newtonsoft.Json.JsonConvert.DeserializeObject (data, typeof(Result));
}
}
public class Data {
public string Platform { get; set; }
public string Code { get; set; }
public string Version { get; set; }
}
public class Result
{
public byte[] Compiled { get; set; }
public string Error { get; set; }
}
static class Mgfx
{
public static byte[] RunMGCB(string code, string platform, string winePath, string compilerPath, out string error)
{
string[] platforms = new string[]
{
"DesktopGL",
"Android",
"iOS",
"tvOS",
"OUYA",
};
var profile = platforms.Contains(platform) ? "OpenGL" : "DirectX_11";
var tempPath = Path.GetFileName( Path.ChangeExtension(Path.GetTempFileName (), ".fx"));
var xnb = Path.ChangeExtension (tempPath, ".mgfx");
var tempOutput = Path.GetTempPath ();
File.WriteAllText(Path.Combine(tempOutput, tempPath), code);
error = String.Empty;
string homeDir = Environment.GetEnvironmentVariable("HOME");
string programPath = $"{homeDir}{compilerPath}";
string effectDir = $"/tmp/{tempPath}";
string mgfxDir = $"/tmp/{xnb}";
effectDir = effectDir.Replace("/", "\\");
mgfxDir = mgfxDir.Replace("/", "\\");
string parameters = string.Format("{0} \"{1}\" \"{2}\" \"{3}\" /Profile:{4}", winePath, programPath, effectDir, mgfxDir, profile);
parameters = parameters.Replace("\"", "\'");
Process proc = new Process();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = string.Format("-c \"{0}\"", parameters);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardError = true;
var stdoutCompleted = new ManualResetEvent(false);
proc.Start();
var response = new System.Text.StringBuilder();
var responseErr = new System.Text.StringBuilder();
while (!proc.StandardOutput.EndOfStream){
response.AppendLine(proc.StandardOutput.ReadLine());
}
while (!proc.StandardError.EndOfStream)
{
responseErr.AppendLine(proc.StandardError.ReadLine());
}
if (response.ToString().Contains("Compiled")){
stdoutCompleted.Set();
}else{
error = responseErr.ToString();
throw new Exception (error);
}
try {
proc.WaitForExit ();
if (File.Exists (Path.Combine (tempOutput, xnb))) {
return File.ReadAllBytes (Path.Combine(tempOutput, xnb));
}
} catch (Exception ex) {
error = ex.ToString ();
throw new Exception (error);
}
finally {
File.Delete (Path.Combine(tempOutput, tempPath));
File.Delete (Path.Combine(tempOutput, xnb));
}
if (proc.ExitCode != 0)
{
throw new InvalidContentException();
}
return new byte[0];
}
}
}