Skip to content

Commit

Permalink
Fix Win32 plugin problem
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jan 23, 2021
1 parent 9d892ff commit 8fa58f8
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 32 deletions.
64 changes: 46 additions & 18 deletions BatsWinApi/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static int l_sound_getset(lua_State *L) {

static int l_msgbox(lua_State *L) {
const char* msg = luaL_checkstring(L, 1);
MessageBoxA(NULL, msg, "BlocklyAts Message", MB_ICONINFORMATION);
MessageBoxA(NULL, msg, "BlocklyAts Message", 0);
return 0;
}

Expand Down Expand Up @@ -176,8 +176,13 @@ ATS_API void WINAPI Initialize(int initIndex) {
if (lua_pcall(L, 1, 0, 0) != 0) l_printerr();
}

bool firstElapse = true;
bool schdSetSignal = false, schdSetBeacon = false, schdDoorChange = false;
int sdatSignal; bool sdatDoor; ATS_BEACONDATA sdatBeacon;

ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int *sound) {
if (L == NULL) return { phBrake, phPower, phReverser, 2 };
ATS_HANDLES result = { phBrake, phPower, phReverser, 2 };
if (L == NULL) return result;
bvePanel = panel;
bveSound = sound;
l_setglobalN("__bve_edLocation", vehicleState.Location);
Expand All @@ -189,6 +194,37 @@ ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int
l_setglobalN("__bve_edBpPressure", vehicleState.BpPressure);
l_setglobalN("__bve_edSapPressure", vehicleState.SapPressure);
l_setglobalN("__bve_edCurrent", vehicleState.Current);
if (firstElapse) {
// Let the internal LastSound inside BVE to be -10000
// So that new sounds can start from the first plugin Elapse
for (int i = 0; i < 256; i++) bveSound[i] = -10000;
firstElapse = false;
return result;
}
if (schdDoorChange) {
schdDoorChange = false;
lua_getglobal(L, "__atsapi_doorchange");
lua_pushboolean(L, sdatDoor);
if (lua_pcall(L, 1, 0, 0) != 0) l_printerr();
if (L == NULL) return result;
}
if (schdSetSignal) {
schdSetSignal = false;
lua_getglobal(L, "__atsapi_setsignal");
lua_pushinteger(L, sdatSignal);
if (lua_pcall(L, 1, 0, 0) != 0) l_printerr();
if (L == NULL) return result;
}
if (schdSetBeacon) {
schdSetBeacon = false;
lua_getglobal(L, "__atsapi_setbeacondata");
lua_pushnumber(L, sdatBeacon.Distance);
lua_pushinteger(L, sdatBeacon.Optional);
lua_pushinteger(L, sdatBeacon.Signal);
lua_pushinteger(L, sdatBeacon.Type);
if (lua_pcall(L, 4, 0, 0) != 0) l_printerr();
if (L == NULL) return result;
}
lua_getglobal(L, "__atsapi_elapse");
lua_pushinteger(L, phPower);
lua_pushinteger(L, phBrake);
Expand All @@ -200,7 +236,6 @@ ATS_API ATS_HANDLES WINAPI Elapse(ATS_VEHICLESTATE vehicleState, int *panel, int
// Lt's hope it'll get the plugin out of the error state by chance
return { vSpec.BrakeNotches + 1, 0, 1, 2 };
} else {
ATS_HANDLES result;
result.Power = lua_tointeger(L, -4);
result.Brake = lua_tointeger(L, -3);
result.Reverser = lua_tointeger(L, -2);
Expand Down Expand Up @@ -251,31 +286,24 @@ ATS_API void WINAPI HornBlow(int atsHornBlowIndex) {

ATS_API void WINAPI DoorOpen() {
if (L == NULL) return;
lua_getglobal(L, "__atsapi_doorchange");
lua_pushinteger(L, 1);
if (lua_pcall(L, 1, 0, 0) != 0) l_printerr();
schdDoorChange = true;
sdatDoor = true;
}

ATS_API void WINAPI DoorClose() {
if (L == NULL) return;
lua_getglobal(L, "__atsapi_doorchange");
lua_pushinteger(L, 0);
if (lua_pcall(L, 1, 0, 0) != 0) l_printerr();
schdDoorChange = true;
sdatDoor = false;
}

ATS_API void WINAPI SetSignal(int signal) {
if (L == NULL) return;
lua_getglobal(L, "__atsapi_setsignal");
lua_pushinteger(L, signal);
if (lua_pcall(L, 1, 0, 0) != 0) l_printerr();
schdSetSignal = true;
sdatSignal = signal;
}

ATS_API void WINAPI SetBeaconData(ATS_BEACONDATA beaconData) {
if (L == NULL) return;
lua_getglobal(L, "__atsapi_setbeacondata");
lua_pushnumber(L, beaconData.Distance);
lua_pushinteger(L, beaconData.Optional);
lua_pushinteger(L, beaconData.Signal);
lua_pushinteger(L, beaconData.Type);
if (lua_pcall(L, 4, 0, 0) != 0) l_printerr();
schdSetBeacon = true;
sdatBeacon = beaconData;
}
1 change: 1 addition & 0 deletions BlocklyAtsGui/BlocklyAtsGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="PlatformFunction.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpgradeInfo.cs" />
<Compile Include="WebView2Browser.cs" />
<Compile Include="WinformBrowser.cs" />
<Compile Include="Workspace.cs" />
Expand Down
12 changes: 12 additions & 0 deletions BlocklyAtsGui/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ private void FormMain_Load(object sender, EventArgs e) {
tscbLanguage.Text = I18n.LanguageDisplayList[i].Value;
}
}

Task.Run(async () => {
var info = await UpgradeInfo.FetchOnline(
"https://www.zbx1425.cn/nautilus/projectmeta.xml",
"BlocklyAts"
);
if (info != null) {
this.Invoke((Action)(() => {
info.ShowPromptAsRequired();
}));
}
});
}

private async void mainWebBrowser_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
Expand Down
8 changes: 8 additions & 0 deletions BlocklyAtsGui/PlatformFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public static bool IsWindows() {
}
}

public static void CallBrowser(string url) {
try {
System.Diagnostics.Process.Start(url);
} catch {

}
}

public static void SetWebBrowserFeatures() {
if (!IsWindows()) return;
// don't change the registry if running in-proc inside Visual Studio
Expand Down
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.5.0")]
[assembly: AssemblyVersion("1.0.5.1")]
5 changes: 5 additions & 0 deletions BlocklyAtsGui/Resources/Attribution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ AlertifyJS is a javascript framework for developing pretty browser dialogs and n
Licensed under GPL 3.0.
https://alertifyjs.com/

- User agent identify: UAIdentifier
JavaScript を実行しているブラウザの種類およびそのレンダリングエンジン・ オペレーティングシステムの種類を判別します。© 2006-2020 Magicant
uai.js は自由に使用していただいて構いません。
http://magicant.github.io/sjavascript/uai-spec.html

- Optional browser interface: CefSharp
Full Web Browser in your .Net App.
Licensed under the 3-Clause BSD License.
Expand Down
3 changes: 2 additions & 1 deletion BlocklyAtsGui/Resources/lang_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Msg.CompileFail = Compilation failed:\n
Msg.NoGameProgram = Please specify game installation path in "Compiler Configuration".
Msg.NotSaved = Not yet saved
Msg.DiscardChange = All unsaved change will be discarded. Confirm?
Msg.LanguageChange = Restart the program to apply language change.
Msg.LanguageChange = Restart the program to apply language change.
Msg.UpgradeAvailable = New version {0} has been released.\nUsing the latest version is recommended, to avoid bugs.\nDownload link: {1} (Press "OK" to open)\n\n{2}
3 changes: 2 additions & 1 deletion BlocklyAtsGui/Resources/lang_ja.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Msg.CompileFail = コンパイルに失敗しました:\n
Msg.NoGameProgram = 「コンパイラ設定」でゲームのパスを指定してください。
Msg.NotSaved = 保存されていません
Msg.DiscardChange = 保存されていない変更はすべて破棄されます。よろしいですか?
Msg.LanguageChange = プログラムを再起動して、言語の変更を適用します。
Msg.LanguageChange = プログラムを再起動して、言語の変更を適用します。
Msg.UpgradeAvailable = 新しいバージョン {0} がリリースされました。\nバグを減らすために、最新バージョンを使用することをお勧めします。\nダウンロードリンク: {1} (「OK」を押して開きます)\n\n{2}
3 changes: 2 additions & 1 deletion BlocklyAtsGui/Resources/lang_zh_CHS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Msg.CompileFail = 编译失败:\n
Msg.NoGameProgram = 请先在 "编译设置" 中指定游戏程序路径。
Msg.NotSaved = 尚未保存
Msg.DiscardChange = 未保存的更改将被放弃。确认?
Msg.LanguageChange = 重新启动程序以应用语言更改。
Msg.LanguageChange = 重新启动程序以应用语言更改。
Msg.UpgradeAvailable = 新版本 {0} 已推出。\n建议您使用新版本,以避免问题。\n下载链接: {1} (按确认键打开)\n\n{2}
59 changes: 59 additions & 0 deletions BlocklyAtsGui/UpgradeInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace BlocklyAts {
public class UpgradeInfo {

public string Project;
public Version LatestVersion;
public string Changelog;
public string WebUrl;

public static async Task<UpgradeInfo> FetchOnline(string url, string name) {
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;

using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream)) {
var xdoc = XDocument.Parse(reader.ReadToEnd());
foreach (var project in xdoc.Root.Elements("Project")) {
if (project.Attribute("Name").Value == name) {
return new UpgradeInfo() {
Project = name,
LatestVersion = new Version(project.Element("LatestVersion").Value),
Changelog = project.Element("Changelog").Value.Replace("\\n", Environment.NewLine),
WebUrl = project.Element("WebUrl").Value
};
}
}
}

return null;
} catch {
// Failing to get upgrade info is nothing serious.
return null;
}
}

public void ShowPromptAsRequired() {
if (LatestVersion > Assembly.GetExecutingAssembly().GetName().Version) {
if (MessageBox.Show(
string.Format(I18n.Translate("Msg.UpgradeAvailable"), LatestVersion.ToString(), WebUrl, Changelog),
"Upgrade Available", MessageBoxButtons.OKCancel, MessageBoxIcon.Information
) == DialogResult.OK) {
PlatformFunction.CallBrowser(WebUrl);
}
}
}
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ AlertifyJS is a javascript framework for developing pretty browser dialogs and n
Licensed under GPL 3.0.
https://alertifyjs.com/

- User agent identify: UAIdentifier
JavaScript を実行しているブラウザの種類およびそのレンダリングエンジン・ オペレーティングシステムの種類を判別します。© 2006-2020 Magicant
uai.js は自由に使用していただいて構いません。
http://magicant.github.io/sjavascript/uai-spec.html

- Optional browser interface: CefSharp
Full Web Browser in your .Net App.
Licensed under the 3-Clause BSD License.
Expand Down
66 changes: 66 additions & 0 deletions assets/lib/boilerplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OpenBveApi.Runtime;

public class LU : List<U> {
public override string ToString() {
return "{" + string.Join(",", this.Select(t => t.ToString())) + "}";
}
}

public class U {
private object e;
public U(object d) { e = d; }

public dynamic Cast(Type type) {
if (e.GetType() == type) return e;
if (type == typeof(bool)) {
if (e is string) {
return (e as string).ToLowerInvariant() == "true" || (e as string) == "1";
} else if (e is int || e is double) {
return (int)e != 0;
} else {
throw new InvalidCastException();
}
} else if (type == typeof(int)) {
if (e is string) {
int result;
if (!int.TryParse(e as string, out result)) throw new InvalidCastException();
return result;
} else if (e is int || e is double) {
return (int)e;
} else {
throw new InvalidCastException();
}
} else if (type == typeof(double)) {
if (e is string) {
double result;
if (!double.TryParse(e as string, out result)) throw new InvalidCastException();
return result;
} else if (e is int || e is double) {
return (double)e;
} else {
throw new InvalidCastException();
}
} else if (type == typeof(string)) {
return e.ToString();
} else if (type == typeof(LU)) {
throw new InvalidCastException();
} else {
throw new InvalidCastException();
}
}

public static implicit operator U(bool d) => new U(d);
public static implicit operator U(int d) => new U(d);
public static implicit operator U(double d) => new U(d);
public static implicit operator U(string d) => new U(d);
public static implicit operator U(LU d) => new U(d);

public static implicit operator bool(U d) => d.Cast(typeof(bool));
public static implicit operator int(U d) => d.Cast(typeof(int));
public static implicit operator double(U d) => d.Cast(typeof(double));
public static implicit operator string(U d) => d.Cast(typeof(string));
public static implicit operator LU(U d) => d.Cast(typeof(LU));
}
5 changes: 5 additions & 0 deletions assets/lib/boilerplate.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local LIP={}function LIP.load(b)assert(type(b)=='string','Parameter \"fileName\" must be a string.')local c=assert(io.open(b,'r'),'Error loading file : '..b)local d={}local e;for f in c:lines()do local g=f:match('^%[([^%[%]]+)%]$')if g then e=tonumber(g)and tonumber(g)or g;d[e]=d[e]or{}end;local h,i=f:match('^([%w|_]+)%s-=%s-(.+)$')if h and i~=nil then if tonumber(i)then i=tonumber(i)elseif i=='true'then i=true elseif i=='false'then i=false end;if tonumber(h)then h=tonumber(h)end;d[e][h]=i end end;c:close()return d end;function LIP.save(b,d)assert(type(b)=='string','Parameter \"fileName\" must be a string.')assert(type(d)=='table','Parameter \"data\" must be a table.')local c=assert(io.open(b,'w+b'),'Error loading file :'..b)local j=''for e,h in pairs(d)do j=j..('[%s]\\n'):format(e)for k,i in pairs(h)do j=j..('%s=%s\\n'):format(k,tostring(i))end;j=j..'\\n'end;c:write(j)c:close()end

__bve_keystate = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}
__bve_doorstate = false

9 changes: 5 additions & 4 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<label text="%{BKY_BVE_MSG_NULL_LOAD}"></label>
<sep gap="8"></sep>
<block type="bve_vehicle_spec"></block>
<sep gap="20"></sep>
<label text="%{BKY_BVE_MSG_ELAPSE_ONLY}"></label>
<sep gap="8"></sep>
<block type="bve_set_handle"></block>
<sep gap="20"></sep>
<label text="%{BKY_BVE_MSG_NULL_LOAD_INIT}"></label>
<sep gap="8"></sep>
<block type="bve_location"></block>
Expand All @@ -88,10 +93,6 @@
</value>
</block>
<block type="bve_get_door"></block>
<sep gap="40"></sep>
<label text="%{BKY_BVE_MSG_ELAPSE_ONLY}"></label>
<sep gap="8"></sep>
<block type="bve_set_handle"></block>
<block type="bve_set_panel"></block>
<block type="bve_get_panel"></block>
<block type="bve_sound_stop"></block>
Expand Down
9 changes: 7 additions & 2 deletions www/js/bats_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ function batsInit(toolboxNode) {

var toolboxMap = [11,0,1,2,3,5,6,7,8,9];
var onkeydown = function(e) {
if (e.shiftKey && e.code[5] >= '0' && e.code[5] <= '9') {
workspace.getToolbox().selectItemByPosition(toolboxMap[parseInt(e.code[5])]);
if (e.shiftKey) {
if (e.code && (e.code[5] >= '0' && e.code[5] <= '9')) {
workspace.getToolbox().selectItemByPosition(toolboxMap[parseInt(e.code[5])]);
} else if (e.keyCode && (e.keyCode >= 0x30 && e.keyCode <= 0x39)) {
// Damn Microsoft!
workspace.getToolbox().selectItemByPosition(toolboxMap[e.keyCode - 0x30]);
}
}
}
document.addEventListener('keydown', onkeydown, false);
Expand Down
2 changes: 1 addition & 1 deletion www/js/lua_generator_bve.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function batsExportLua(workspace) {
"bve_hat_door_change", "bve_hat_set_signal", "bve_hat_set_beacon", "bve_hat_load", "bve_hat_dispose"];
var code = "local LIP={}function LIP.load(b)assert(type(b)=='string','Parameter \"fileName\" must be a string.')local c=assert(io.open(b,'r'),'Error loading file : '..b)local d={}local e;for f in c:lines()do local g=f:match('^%[([^%[%]]+)%]$')if g then e=tonumber(g)and tonumber(g)or g;d[e]=d[e]or{}end;local h,i=f:match('^([%w|_]+)%s-=%s-(.+)$')if h and i~=nil then if tonumber(i)then i=tonumber(i)elseif i=='true'then i=true elseif i=='false'then i=false end;if tonumber(h)then h=tonumber(h)end;d[e][h]=i end end;c:close()return d end;function LIP.save(b,d)assert(type(b)=='string','Parameter \"fileName\" must be a string.')assert(type(d)=='table','Parameter \"data\" must be a table.')local c=assert(io.open(b,'w+b'),'Error loading file :'..b)local j=''for e,h in pairs(d)do j=j..('[%s]\\n'):format(e)for k,i in pairs(h)do j=j..('%s=%s\\n'):format(k,tostring(i))end;j=j..'\\n'end;c:write(j)c:close()end\n" +
"__bve_keystate={false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}\n" +
"__bve_doorstate=0\n";
"__bve_doorstate=false\n";
var blocks = workspace.getTopBlocks(false);
for (var i = 0, block; block = blocks[i]; i++) {
if (block.type.startsWith("bve_hat")) {
Expand Down
Loading

0 comments on commit 8fa58f8

Please sign in to comment.