Skip to content

Commit 132c178

Browse files
committed
Added inputs, outputs, vector library, tons of entity stuff
1 parent e73014d commit 132c178

File tree

11 files changed

+2455
-148
lines changed

11 files changed

+2455
-148
lines changed

c++_module/js.cpp

Lines changed: 2203 additions & 23 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1-
AddCSLuaFile()
1+
AddCSLuaFile()
2+
3+
-- Borrowed from facepunch lmao
4+
5+
local function AddFile( File, directory )
6+
local prefix = string.lower( string.Left( File, 3 ) )
7+
8+
if SERVER and prefix == "sv_" then
9+
include( directory .. File )
10+
elseif prefix == "sh_" then
11+
if SERVER then
12+
AddCSLuaFile( directory .. File )
13+
end
14+
include( directory .. File )
15+
elseif prefix == "cl_" then
16+
if SERVER then
17+
AddCSLuaFile( directory .. File )
18+
elseif CLIENT then
19+
include( directory .. File )
20+
end
21+
end
22+
end
23+
24+
local function IncludeDir( directory )
25+
directory = directory .. "/"
26+
27+
local files, directories = file.Find( directory .. "*", "LUA" )
28+
29+
for _, v in ipairs( files ) do
30+
if string.EndsWith( v, ".lua" ) then
31+
AddFile( v, directory )
32+
end
33+
end
34+
35+
for _, v in ipairs( directories ) do
36+
IncludeDir( directory .. v )
37+
end
38+
end
39+
40+
if SERVER then
41+
require("javascript");
42+
end
43+
44+
IncludeDir( "wire/jschip" )

wire-js-chip_2/lua/entities/gmod_wire_jschip.lua

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ ENT.WireDebugName = "JSCHIP"
66

77
if CLIENT then return end
88

9-
require("javascript");
10-
11-
JSChatPrint = function(ply, ...)
12-
if CurTime() < (ply.JSCanPrint or 0) then return end
13-
14-
net.Start("WireExpression2_BetterChatPrint")
15-
net.WriteString(string.sub(table.concat({ ... }, "\t"), 1, 64000))
16-
net.Send(ply)
17-
18-
ply.JSCanPrint = CurTime() + 0.2
19-
end
20-
219
function ENT:Initialize()
2210
self:PhysicsInit(SOLID_VPHYSICS)
2311
self:SetMoveType(MOVETYPE_VPHYSICS)
@@ -89,7 +77,45 @@ function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
8977
self.ctx = ctx
9078
end
9179

80+
function ENT:SetInputs(inputTable)
81+
local names = {}
82+
local types = {}
83+
84+
for key, value in pairs(inputTable) do
85+
table.insert(names, key)
86+
table.insert(types, string.upper(value))
87+
end
88+
89+
self.Inputs = WireLib.AdjustSpecialInputs(self, names, types)
90+
end
91+
92+
function ENT:SetOutputs(outputTable)
93+
local names = {}
94+
local types = {}
95+
96+
for key, value in pairs(outputTable) do
97+
table.insert(names, key)
98+
table.insert(types, string.upper(value))
99+
end
100+
101+
self.Outputs = WireLib.AdjustSpecialOutputs(self, names, types)
102+
end
103+
104+
function ENT:TriggerOutput(iname, value)
105+
WireLib.TriggerOutput(self, iname, value)
106+
end
107+
108+
function ENT:OutputType(iname)
109+
return (self.Outputs[iname] and self.Outputs[iname].Type) or "none"
110+
end
111+
92112
function ENT:TriggerInput(iname, value)
113+
if self.Run then
114+
local err, result = JS_EmitEvent(self.ctx, "input", iname, value)
115+
if err ~= 0 then
116+
self:Error(result)
117+
end
118+
end
93119
end
94120

95121
duplicator.RegisterEntityClass("gmod_wire_jschip", WireLib.MakeWireEnt, "Data")

wire-js-chip_2/lua/wire/client/text_editor/modes/jschip.lua

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,6 @@ local string_gmatch = string.gmatch
33
local string_gsub = string.gsub
44
local draw_WordBox = draw.WordBox
55

6-
function JSChip_UploadCode(script, validate)
7-
local chunkSize = 64000
8-
local chunks = {}
9-
10-
for i = 1, #script, chunkSize do
11-
chunks[#chunks + 1] = script:sub(i, i + chunkSize - 1)
12-
end
13-
14-
for i, chunk in ipairs(chunks) do
15-
if validate then
16-
net.Start("JSChip_ValidateCode")
17-
else
18-
net.Start("JSChip_RequestCode")
19-
end
20-
net.WriteUInt(#chunks, 16)
21-
net.WriteUInt(i, 16)
22-
net.WriteString(chunk)
23-
net.SendToServer()
24-
end
25-
end
26-
27-
net.Receive("JSChip_ValidateCode", function(len, ply)
28-
local err = net.ReadBool()
29-
local result = net.ReadString()
30-
if err then
31-
JS_Editor.C.Val:Update({{message = result, line = nil, char = nil}}, nil, result, Color(128, 20, 50))
32-
else
33-
JS_Editor.C.Val:Update(nil, nil, " Validation successful ", Color(50, 128, 20))
34-
end
35-
end)
36-
37-
net.Receive("JSChip_RequestCode", function(len, ply)
38-
if JS_Editor then
39-
JSChip_UploadCode(JS_Editor:GetCode(), false)
40-
end
41-
end)
42-
43-
function JSChip_Validate(editor,source,fileName)
44-
JSChip_UploadCode(source, true)
45-
end
46-
476
local EDITOR = {
487
UseValidator = true,
498
Validator = JSChip_Validate,
@@ -247,7 +206,8 @@ local stdObjectTable = {
247206
["Events"] = true,
248207
["Inputs"] = true,
249208
["Outputs"] = true,
250-
["Entity"] = true
209+
["Entity"] = true,
210+
["Vector"] = true
251211
}
252212

253213
function EDITOR:CommentSelection(removecomment)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
net.Receive("JSChip_ChatPrint", function()
2+
chat.AddText(net.ReadString())
3+
end)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
util.AddNetworkString("JSChip_ChatPrint")
2+
3+
JSChatPrint = function(ply, ...)
4+
if CurTime() < (ply.JSCanPrint or 0) then return end
5+
6+
net.Start("JSChip_ChatPrint")
7+
net.WriteString(string.sub(table.concat({ ... }, "\t"), 1, 64000))
8+
net.Send(ply)
9+
10+
ply.JSCanPrint = CurTime() + 0.2
11+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function JSChip_UploadCode(script, validate)
2+
local chunkSize = 64000
3+
local chunks = {}
4+
5+
for i = 1, #script, chunkSize do
6+
chunks[#chunks + 1] = script:sub(i, i + chunkSize - 1)
7+
end
8+
9+
for i, chunk in ipairs(chunks) do
10+
if validate then
11+
net.Start("JSChip_ValidateCode")
12+
else
13+
net.Start("JSChip_RequestCode")
14+
end
15+
net.WriteUInt(#chunks, 16)
16+
net.WriteUInt(i, 16)
17+
net.WriteString(chunk)
18+
net.SendToServer()
19+
end
20+
end
21+
22+
function JSChip_Validate(editor, source, fileName)
23+
JSChip_UploadCode(source, true)
24+
end
25+
26+
net.Receive("JSChip_ValidateCode", function(len, ply)
27+
local err = net.ReadBool()
28+
local result = net.ReadString()
29+
if err then
30+
JS_Editor.C.Val:Update({{message = result, line = nil, char = nil}}, nil, result, Color(128, 20, 50))
31+
else
32+
JS_Editor.C.Val:Update(nil, nil, " Validation successful ", Color(50, 128, 20))
33+
end
34+
end)
35+
36+
net.Receive("JSChip_RequestCode", function(len, ply)
37+
if JS_Editor then
38+
JSChip_UploadCode(JS_Editor:GetCode(), false)
39+
end
40+
end)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
util.AddNetworkString("JSChip_RequestCode")
2+
3+
local scriptChunks = {}
4+
5+
net.Receive("JSChip_RequestCode", function(len, ply)
6+
local totalChunks = net.ReadUInt(16)
7+
local chunkIndex = math.Clamp(net.ReadUInt(16), 1, totalChunks)
8+
local chunkData = net.ReadString()
9+
local chip = ply.JSChipTarget
10+
11+
if not IsValid(chip) then return end
12+
13+
scriptChunks[ply] = scriptChunks[ply] or {chunks = {}, total = 0}
14+
15+
scriptChunks[ply].chunks[chunkIndex] = chunkData
16+
scriptChunks[ply].total = totalChunks
17+
18+
local storedChunks = scriptChunks[ply].chunks
19+
if table.Count(storedChunks) == totalChunks then
20+
local completeScript = table.concat(storedChunks)
21+
scriptChunks[ply] = nil
22+
23+
local err, bytecode, length = JS_Compile(chip.ctx, #completeScript, completeScript)
24+
25+
if err ~= 0 then
26+
chip:Error(bytecode)
27+
else
28+
chip.Run = true
29+
chip:SetColor(Color(255, 255, 255))
30+
31+
local err, result = JS_Eval(chip.ctx, length, bytecode)
32+
if err ~= 0 then
33+
chip:Error(result)
34+
end
35+
36+
chip.script = completeScript
37+
end
38+
end
39+
end)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function JSChip_UploadCode(script, validate)
2+
local chunkSize = 64000
3+
local chunks = {}
4+
5+
for i = 1, #script, chunkSize do
6+
chunks[#chunks + 1] = script:sub(i, i + chunkSize - 1)
7+
end
8+
9+
for i, chunk in ipairs(chunks) do
10+
if validate then
11+
net.Start("JSChip_ValidateCode")
12+
else
13+
net.Start("JSChip_RequestCode")
14+
end
15+
net.WriteUInt(#chunks, 16)
16+
net.WriteUInt(i, 16)
17+
net.WriteString(chunk)
18+
net.SendToServer()
19+
end
20+
end
21+
22+
net.Receive("JSChip_ValidateCode", function(len, ply)
23+
local err = net.ReadBool()
24+
local result = net.ReadString()
25+
if err then
26+
JS_Editor.C.Val:Update({{message = result, line = nil, char = nil}}, nil, result, Color(128, 20, 50))
27+
else
28+
JS_Editor.C.Val:Update(nil, nil, " Validation successful ", Color(50, 128, 20))
29+
end
30+
end)
31+
32+
net.Receive("JSChip_RequestCode", function(len, ply)
33+
if JS_Editor then
34+
JSChip_UploadCode(JS_Editor:GetCode(), false)
35+
end
36+
end)
37+
38+
function JSChip_Validate(editor,source,fileName)
39+
JSChip_UploadCode(source, true)
40+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
util.AddNetworkString("JSChip_ValidateCode")
2+
3+
local scriptChunks = {}
4+
5+
net.Receive("JSChip_ValidateCode", function(len, ply)
6+
local totalChunks = net.ReadUInt(16)
7+
local chunkIndex = math.Clamp(net.ReadUInt(16), 1, totalChunks)
8+
local chunkData = net.ReadString()
9+
10+
scriptChunks[ply] = scriptChunks[ply] or {chunks = {}, total = 0}
11+
12+
scriptChunks[ply].chunks[chunkIndex] = chunkData
13+
scriptChunks[ply].total = totalChunks
14+
15+
local storedChunks = scriptChunks[ply].chunks
16+
if table.Count(storedChunks) == totalChunks then
17+
local completeScript = table.concat(storedChunks)
18+
scriptChunks[ply] = nil
19+
20+
local err, ctx = JS_CreateContext(ply)
21+
if err ~= 0 then
22+
net.Start("JSChip_ValidateCode")
23+
net.WriteBool(true, 16)
24+
net.WriteString("failed to validate the script")
25+
net.Send(ply)
26+
else
27+
local err, bytecode, length = JS_Compile(ctx, #completeScript, completeScript)
28+
net.Start("JSChip_ValidateCode")
29+
net.WriteBool(err ~= 0, 16)
30+
net.WriteString(bytecode)
31+
net.Send(ply)
32+
JS_FreeContext(ctx)
33+
end
34+
end
35+
end)

0 commit comments

Comments
 (0)