Skip to content

Commit

Permalink
Add auto type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed May 22, 2021
1 parent 6196881 commit a2a44c9
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 34 deletions.
24 changes: 12 additions & 12 deletions BlocklyAtsGui/BlocklyAtsGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
<Reference Include="FastColoredTextBox, Version=2.16.24.0, Culture=neutral, PublicKeyToken=fb8aa12b994ef61b, processorArchitecture=MSIL">
<HintPath>..\packages\FCTB.2.16.24\lib\FastColoredTextBox.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.664.37, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.664.37\lib\net462\Microsoft.Web.WebView2.Core.dll</HintPath>
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.818.41, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.818.41\lib\net45\Microsoft.Web.WebView2.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.664.37, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.664.37\lib\net462\Microsoft.Web.WebView2.WinForms.dll</HintPath>
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.818.41, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.818.41\lib\net45\Microsoft.Web.WebView2.WinForms.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.664.37, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.664.37\lib\net462\Microsoft.Web.WebView2.Wpf.dll</HintPath>
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.818.41, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.818.41\lib\net45\Microsoft.Web.WebView2.Wpf.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -208,15 +208,15 @@ COPY /Y "$(SolutionDir)AtsCallConverter\bin\Debug\x86\AtsCallConverter.dll" "$(T
COPY /Y "$(SolutionDir)AtsCallConverter\bin\Debug\x64\AtsCallConverter.dll" "$(TargetDir)\lib\AtsCallConverter_x64.dll"
)</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.818.41\build\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.818.41\build\Microsoft.Web.WebView2.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.818.41\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.818.41\build\Microsoft.Web.WebView2.targets'))" />
</Target>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets')" />
</Project>
2 changes: 1 addition & 1 deletion BlocklyAtsGui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.100.101")]
[assembly: AssemblyVersion("1.0.100.102")]
1 change: 1 addition & 0 deletions BlocklyAtsGui/UserInterface/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ private void tsbtnUserConfig_Click(object sender, EventArgs e) {
if (result == DialogResult.Abort) {
// How can I stop WebView2 from occupying the files?
// I cannot delete the entire DataDirectory
mainWebBrowser.Dispose();
if (File.Exists(PreferenceManager.PreferencePath))
File.Delete(PreferenceManager.PreferencePath);
if (!PlatformFunction.IsMono)
Expand Down
2 changes: 1 addition & 1 deletion BlocklyAtsGui/WebView/WebView2Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public WebView2Browser(string url = "about:blank") {
var createTask = CoreWebView2Environment.CreateAsync(null, PreferenceManager.WebView2UserDataPath, null);
createTask.Wait();
environment = createTask.Result;
browser.CoreWebView2Ready += (sender, e) => {
browser.CoreWebView2InitializationCompleted += (sender, e) => {
browser.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
browser.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
browser.CoreWebView2.Settings.IsZoomControlEnabled = false;
Expand Down
2 changes: 1 addition & 1 deletion BlocklyAtsGui/Workspace/CompilerFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static string CombineCodeForCSharp(string script, bool includeOpenBve) {
sb.Append("namespace BlocklyAts {\n");
sb.Append("\n\n// ----- Start of your program. -----\n\n");
sb.Append(script);
sb.Append("\n\n// ----- End of your program. -----\n\n\n");
sb.Append("\n\n// ----- End of your program. You don't have to care about codes after this line. -----\n\n\n");
sb.Append(CodeFunction);
if (includeOpenBve) sb.Append(CodeOpenBve);
sb.Append("}\n");
Expand Down
2 changes: 1 addition & 1 deletion BlocklyAtsGui/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FCTB" version="2.16.24" targetFramework="net462" />
<package id="Microsoft.Web.WebView2" version="1.0.664.37" targetFramework="net462" />
<package id="Microsoft.Web.WebView2" version="1.0.818.41" targetFramework="net462" />
</packages>
103 changes: 103 additions & 0 deletions assets/lib/function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,106 @@ public void MsgBox(string text) {
MessageBox.Show(text, "BlocklyATS Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

// This helper class simulates the behavior of implicit type conversion like Javascript.
public static class C {

public static int Int(object src) {
try {
try {
return Convert.ToInt32(src);
} catch {
return (int)Convert.ToDouble(src);
}
} catch (FormatException ex) {
throw new FormatException("\"" + src.ToString() + "\" is not numeric; " + ex.ToString());
}
}

public static double Double(object src) {
try {
return Convert.ToDouble(src);
} catch (FormatException ex) {
throw new FormatException("\"" + src.ToString() + "\" is not numeric; " + ex.ToString());
}
}

public static bool CanConvertToDouble(object src) {
try {
Convert.ToDouble(src);
return true;
} catch {
return false;
}
}

public static bool Bool(object src) {
try {
return Convert.ToBoolean(src);
} catch (FormatException ex) {
throw new FormatException("\"" + src.ToString() + "\" cannot be represented by Boolean; " + ex.ToString());
}
}

public static bool CanConvertToBool(object src) {
try {
Convert.ToBoolean(src);
return true;
} catch {
return false;
}
}

public static dynamic Add(dynamic a, dynamic b) {
if (IsNumeric(a) && IsNumeric(b)) {
return Double(a) + Double(b);
} else {
return a + b;
}
}

public static dynamic Sub(dynamic a, dynamic b) {
if (IsNumeric(a) && IsNumeric(b)) {
return Double(a) - Double(b);
} else {
return a - b;
}
}

public static dynamic Mul(dynamic a, dynamic b) {
if (IsNumeric(a) && IsNumeric(b)) {
return Double(a) * Double(b);
} else {
return a * b;
}
}
public static dynamic Div(dynamic a, dynamic b) {
if (IsNumeric(a) && IsNumeric(b)) {
return Double(a) / Double(b);
} else {
return a / b;
}
}

public static bool IsNumeric(object o) {
switch (Type.GetTypeCode(o.GetType())) {
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
case TypeCode.String:
double discard;
return double.TryParse(o.ToString(), out discard);
default:
return false;
}
}
}
1 change: 1 addition & 0 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@
<block type="bve_convert_to_double"></block>
<block type="bve_convert_to_string"></block>
<block type="bve_convert_to_boolean"></block>
<block type="bve_can_convert_to"></block>
<sep gap="40"></sep>
<block type="bve_msgbox">
<value name="MSG">
Expand Down
20 changes: 20 additions & 0 deletions www/js/blocks_bve.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,26 @@ Blockly.defineBlocksWithJsonArray([
],
output: "Boolean"
},
{
type: "bve_can_convert_to",
style: "logic_blocks",
message0: "%{BKY_BVE_CAN_CONVERT_TO}",
args0: [
{
type: "input_value",
name: "SOURCE"
},
{
type: "field_dropdown",
name: "TYPE",
options: [
["%{BKY_BVE_TYPE_DOUBLE}", "Double"],
["%{BKY_BVE_TYPE_BOOLEAN}", "Bool"]
]
}
],
output: "Boolean"
},
{
type: "bve_comment",
style: "comment_block",
Expand Down
Binary file modified www/js/csharp_compressed.js
Binary file not shown.
79 changes: 64 additions & 15 deletions www/js/csharp_generator_bve.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,50 @@ Blockly.CSharp.addReservedWords([
"_p2",
].join(","));

if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
// Could we try:
// return Array(count + 1).join(this);
return rpt;
}
}

function indentString(str, count, shorterfirst) {
if (!count) count = 1;
var result = str.replace(/^/gm, " ".repeat(count));
Expand Down Expand Up @@ -135,41 +179,41 @@ Blockly.CSharp.bve_get_handle=function(block){
}
Blockly.CSharp.bve_set_handle=function(block){
var handleID = ["Brake", "Power", "Reverser", "ConstSpeed"].indexOf(block.getFieldValue("FIELD_SEL"));
return "_c.SetHandle(" + handleID + ", (int)("
return "_c.SetHandle(" + handleID + ", C.Int("
+ (Blockly.CSharp.valueToCode(block, "VALUE", Blockly.CSharp.ORDER_NONE) || "0") + "));\n";
}
Blockly.CSharp.bve_sound_stop=function(block){
return "_c.SetLegacySound((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), -10000);\n";
return "_c.SetLegacySound(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), -10000);\n";
}
Blockly.CSharp.bve_sound_play_once=function(block){
return "_c.SetLegacySound((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), 1);\n";
return "_c.SetLegacySound(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), 1);\n";
}
Blockly.CSharp.bve_sound_play_loop=function(block){
return "_c.SetLegacySoundLV((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), (double)(" +
return "_c.SetLegacySoundLV(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), C.Double(" +
Blockly.CSharp.valueToCode(block, "VOLUME", Blockly.CSharp.ORDER_NONE) + "));\n";
}
Blockly.CSharp.bve_get_sound_internal=function(block){
return ["_c.GetLegacySound((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "))",
return ["_c.GetLegacySound(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "))",
Blockly.CSharp.ORDER_ATOMIC];
}
Blockly.CSharp.bve_set_sound_internal=function(block){
return "_c.SetLegacySound((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), (int)(" +
return "_c.SetLegacySound(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), C.Int(" +
Blockly.CSharp.valueToCode(block, "INTERNAL_VAL", Blockly.CSharp.ORDER_NONE) + "));\n";
}
Blockly.CSharp.bve_set_panel=function(block){
return "_c.SetPanel((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), (int)(" +
return "_c.SetPanel(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "), C.Int(" +
Blockly.CSharp.valueToCode(block, "VALUE", Blockly.CSharp.ORDER_NONE) + "));\n";
}
Blockly.CSharp.bve_get_panel=function(block){
return ["_c.GetPanel((int)(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "))",
return ["_c.GetPanel(C.Int(" + Blockly.CSharp.valueToCode(block, "ID", Blockly.CSharp.ORDER_NONE) + "))",
Blockly.CSharp.ORDER_ATOMIC];
}
Blockly.CSharp.bve_key=function(block){
return [["S","A1","A2","B1","B2","C1","C2","D","E","F","G","H","I","J","K","L"].indexOf(block.getFieldValue("KEY_TYPE")),
Blockly.CSharp.ORDER_ATOMIC];
}
Blockly.CSharp.bve_get_key=function(block){
return ["_c.KeyState[(int)(" + Blockly.CSharp.valueToCode(block, "KEY_TYPE", Blockly.CSharp.ORDER_NONE) + ")]",
return ["_c.KeyState[C.Int(" + Blockly.CSharp.valueToCode(block, "KEY_TYPE", Blockly.CSharp.ORDER_NONE) + ")]",
Blockly.CSharp.ORDER_MEMBER];
}
Blockly.CSharp.bve_horn=function(block){
Expand Down Expand Up @@ -230,8 +274,8 @@ Blockly.CSharp.bve_hat_timer=function(block){
Blockly.CSharp.bve_timer_set=function(block){
var timerName = Blockly.CSharp.bveTimerNameDB.getName(block.getFieldValue("NAME"), Blockly.Generator.NAME_TYPE);
return "_f.SetTimer("
+ Blockly.CSharp.quote_(timerName) + ", (int)("
+ Blockly.CSharp.valueToCode(block, "INTERVAL", Blockly.CSharp.ORDER_NONE) + "), (bool)("
+ Blockly.CSharp.quote_(timerName) + ", C.Int("
+ Blockly.CSharp.valueToCode(block, "INTERVAL", Blockly.CSharp.ORDER_NONE) + "), C.Bool("
+ Blockly.CSharp.valueToCode(block, "CYCLE", Blockly.CSharp.ORDER_NONE) + "));\n";
}
Blockly.CSharp.bve_timer_modify=function(block){
Expand All @@ -248,17 +292,22 @@ Blockly.CSharp.bve_timer_modify=function(block){
}
}
Blockly.CSharp.bve_convert_to_double=function(block) {
return ["Convert.ToDouble(" + Blockly.CSharp.valueToCode(block, "SOURCE", Blockly.CSharp.ORDER_NONE) + ")",
return ["C.Double(" + Blockly.CSharp.valueToCode(block, "SOURCE", Blockly.CSharp.ORDER_NONE) + ")",
Blockly.CSharp.ORDER_FUNCTION_CALL];
}
Blockly.CSharp.bve_convert_to_boolean=function(block) {
return ["C.Bool(" + Blockly.CSharp.valueToCode(block, "SOURCE", Blockly.CSharp.ORDER_NONE) + ")",
Blockly.CSharp.ORDER_FUNCTION_CALL];
}
Blockly.CSharp.bve_convert_to_string=function(block) {
return ["Convert.ToString(" + Blockly.CSharp.valueToCode(block, "SOURCE", Blockly.CSharp.ORDER_NONE) + ")",
Blockly.CSharp.ORDER_FUNCTION_CALL];
}
Blockly.CSharp.bve_convert_to_boolean=function(block) {
return ["Convert.ToBoolean(" + Blockly.CSharp.valueToCode(block, "SOURCE", Blockly.CSharp.ORDER_NONE) + ")",
Blockly.CSharp.bve_can_convert_to=function(block) {
return ["C.CanConvertTo" + block.getFieldValue("TYPE") + "(" +
Blockly.CSharp.valueToCode(block, "SOURCE", Blockly.CSharp.ORDER_NONE) + ")",
Blockly.CSharp.ORDER_FUNCTION_CALL];
}
Blockly.CSharp.bve_comment = function(block) { return ""; }
Blockly.CSharp.bve_rawcode_statement = function(block) { return block.getFieldValue("CODE") + "\n"; }
Blockly.CSharp.bve_rawcode_value = function(block) { return block.getFieldValue("CODE"); }
Blockly.CSharp.bve_rawcode_value = function(block) { return block.getFieldValue("CODE"); }
5 changes: 4 additions & 1 deletion www/js/msg_bats/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Blockly.Msg["BVE_TMRMOD_TRIGRESET"]="trigger&restart";
Blockly.Msg["BVE_CONVERT_TO_DOUBLE"]="%1 to number";
Blockly.Msg["BVE_CONVERT_TO_STRING"]="%1 to string";
Blockly.Msg["BVE_CONVERT_TO_BOOLEAN"]="%1 to boolean";
Blockly.Msg["BVE_CAN_CONVERT_TO"]="%1 can be converted to %2?";
Blockly.Msg["BVE_TYPE_DOUBLE"]="number";
Blockly.Msg["BVE_TYPE_BOOLEAN"]="boolean";

Blockly.Msg["BVE_MSG_HAT_SINGLETON"]="Events may only be used once each";
Blockly.Msg["BVE_MSG_HAT_TIMER_SINGLETON"]="Only one 'when triggered' may be used for each timer name.";
Expand All @@ -120,7 +123,7 @@ Blockly.Msg["BVE_PLACEHOLDER_TIMER"]="timer name";
Blockly.Msg["BVE_PLACEHOLDER_COMMENT"]="Comment block. Have no effect.\nInsert at places you want to describe.";

Blockly.Msg["BVE_MSGBOX"]="msgbox %1";
Blockly.Msg["BVE_EXCEPTION"]="error %1";
Blockly.Msg["BVE_EXCEPTION"]="show error %1";

Blockly.Msg["CAT_LOGIC"]="Logics";
Blockly.Msg["CAT_LOOP"]="Loops";
Expand Down
Loading

0 comments on commit a2a44c9

Please sign in to comment.