Skip to content

Commit

Permalink
Made templates into resources to cleanup code.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Forshaw committed May 16, 2017
1 parent b595496 commit 4ebd906
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 111 deletions.
23 changes: 23 additions & 0 deletions DotNetToJScript/DotNetToJScript.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,34 @@
<Compile Include="JScriptGenerator.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="VBAGenerator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="Resources\scriptlet_template.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\jscript_template.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\vba_template.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\jscript_auto_version_script.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
69 changes: 16 additions & 53 deletions DotNetToJScript/JScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,35 @@ namespace DotNetToJScript
{
class JScriptGenerator : IScriptGenerator
{
static string jscript_template =
@"var serialized_obj = [
%SERIALIZED%
];
var entry_class = '%CLASS%';
try {
setversion();
var stm = new ActiveXObject('System.IO.MemoryStream');
var fmt = new ActiveXObject('System.Runtime.Serialization.Formatters.Binary.BinaryFormatter');
var al = new ActiveXObject('System.Collections.ArrayList')
for (i in serialized_obj) {
stm.WriteByte(serialized_obj[i]);
}
stm.Position = 0;
var n = fmt.SurrogateSelector;
var d = fmt.Deserialize_2(stm);
al.Add(n);
var o = d.DynamicInvoke(al.ToArray()).CreateInstance(entry_class);
%ADDEDSCRIPT%
} catch (e) {
debug(e.message);
}";

static string version_detection = @"function setversion() {
var shell = new ActiveXObject('WScript.Shell');
ver = 'v4.0.30319';
try {
shell.RegRead('HKLM\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\');
} catch(e) {
ver = 'v2.0.50727';
}
shell.Environment('Process')('COMPLUS_Version') = ver;
}";
static string version_v2 = @"function setversion() {
new ActiveXObject('WScript.Shell').Environment('Process')('COMPLUS_Version') = 'v2.0.50727';
}";
static string version_v4 = @"function setversion() {
new ActiveXObject('WScript.Shell').Environment('Process')('COMPLUS_Version') = 'v4.0.30319';
}";
static string version_none = @"function setversion() {}";

static string debug_enabled = @"function debug(s) { WScript.Echo(s); }" + Environment.NewLine;
static string debug_disabled = @"function debug(s) { }" + Environment.NewLine;

static string GetSetVersionJScript(RuntimeVersion version)
static string GetScriptHeader(RuntimeVersion version, bool enable_debug)
{
string script = version_none;
StringBuilder builder = new StringBuilder();
builder.AppendLine("function setversion() {");
switch (version)
{
case RuntimeVersion.Auto:
script = version_detection;
builder.AppendLine(Properties.Resources.jscript_auto_version_script);
break;
case RuntimeVersion.v2:
script = version_v2;
builder.AppendLine("new ActiveXObject('WScript.Shell').Environment('Process')('COMPLUS_Version') = 'v2.0.50727';");
break;
case RuntimeVersion.v4:
script = version_v4;
builder.AppendLine("new ActiveXObject('WScript.Shell').Environment('Process')('COMPLUS_Version') = 'v4.0.30319';");
break;
}
return script + Environment.NewLine;
builder.AppendLine("}");
builder.AppendLine("function debug(s) {");
if (enable_debug)
{
builder.AppendLine("WScript.Echo(s);");
}
builder.AppendLine("}");
return builder.ToString();
}


public string ScriptName
{
get
Expand Down Expand Up @@ -120,8 +83,8 @@ public string GenerateScript(byte[] serialized_object, string entry_class_name,
}
}

return (enable_debug ? debug_enabled : debug_disabled) + GetSetVersionJScript(version)
+ jscript_template.Replace("%SERIALIZED%", builder.ToString()).Replace("%CLASS%", entry_class_name).Replace("%ADDEDSCRIPT%", additional_script);
return GetScriptHeader(version, enable_debug)
+ Properties.Resources.jscript_template.Replace("%SERIALIZED%", builder.ToString()).Replace("%CLASS%", entry_class_name).Replace("%ADDEDSCRIPT%", additional_script);
}
}
}
17 changes: 2 additions & 15 deletions DotNetToJScript/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ namespace DotNetToJScript
{
class Program
{
static string scriptlet_template =
@"<?xml version='1.0'?>
<package>
<component id='dummy'>
<registration
description='dummy'
progid='dummy'
version='1.00'
remotable='True'>
</registration>
</component>
</package>
";
enum ScriptLanguage
{
JScript,
Expand Down Expand Up @@ -91,7 +78,7 @@ static object BuildLoaderDelegateMscorlib(byte[] assembly)
static string CreateScriptlet(string script, string script_name, bool register_script)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(scriptlet_template);
doc.LoadXml(Properties.Resources.scriptlet_template);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
Expand Down Expand Up @@ -248,7 +235,7 @@ static void Main(string[] args)
string script = generator.GenerateScript(stm.ToArray(), entry_class_name, additional_script, version, enable_debug);
if (scriptlet_moniker || scriptlet_uninstall)
{
if (generator.SupportsScriptlet)
if (!generator.SupportsScriptlet)
{
throw new ArgumentException(String.Format("{0} generator does not support Scriptlet output", generator.ScriptName));
}
Expand Down
155 changes: 155 additions & 0 deletions DotNetToJScript/Properties/Resources.Designer.cs

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

Loading

0 comments on commit 4ebd906

Please sign in to comment.