Skip to content

Commit

Permalink
Add lualib
Browse files Browse the repository at this point in the history
  • Loading branch information
Vorlias committed Aug 6, 2019
1 parent 34dc40f commit 721dcf8
Show file tree
Hide file tree
Showing 18 changed files with 2,775 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ function build_rbxmx {
function build_lua {
compile
echo "[net-build] compiling lua output..."
rm -rf lualib
mkdir -p lualib
cp -r out/* lualib
cp -r include lualib/vendor

find dist/lua -name '*.d.ts' -delete
find lualib -name '*.d.ts' -delete
rm -rf lualib/Test

echo "[net-build] Output to ./lualib"
}
Expand Down
45 changes: 45 additions & 0 deletions lualib/ClientEvent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Compiled with https://roblox-ts.github.io v0.2.14
-- August 6, 2019, 5:39 PM New Zealand Standard Time

local TS = require(script.Parent.vendor.RuntimeLib);
local exports = {};
local NetClientEvent;
local _0 = TS.import(script.Parent, "internal");
local getRemoteOrThrow, IS_CLIENT, waitForEvent, MAX_CLIENT_WAITFORCHILD_TIMEOUT = _0.getRemoteOrThrow, _0.IS_CLIENT, _0.waitForEvent, _0.MAX_CLIENT_WAITFORCHILD_TIMEOUT;
do
NetClientEvent = setmetatable({}, {
__tostring = function() return "NetClientEvent" end;
});
NetClientEvent.__index = NetClientEvent;
function NetClientEvent.new(...)
local self = setmetatable({}, NetClientEvent);
self:constructor(...);
return self;
end;
function NetClientEvent:constructor(name)
self.instance = getRemoteOrThrow("RemoteEvent", name);
assert(IS_CLIENT, "Cannot create a Net.ClientEvent on the Server!");
end;
NetClientEvent.WaitFor = TS.async(function(self, name)
local fun = waitForEvent(name, MAX_CLIENT_WAITFORCHILD_TIMEOUT);
if not fun then
error("Failed to retrieve client Event!");
end;
return NetClientEvent.new(name);
end);
function NetClientEvent:getInstance()
return self.instance;
end;
function NetClientEvent:getEvent()
return self.instance.OnClientEvent;
end;
function NetClientEvent:Connect(callback)
return self:getEvent():Connect(callback);
end;
function NetClientEvent:SendToServer(...)
local args = { ... };
self.instance:FireServer(unpack(args));
end;
end;
exports.default = NetClientEvent;
return exports;
67 changes: 67 additions & 0 deletions lualib/ClientFunction.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- Compiled with https://roblox-ts.github.io v0.2.14
-- August 6, 2019, 5:39 PM New Zealand Standard Time

local TS = require(script.Parent.vendor.RuntimeLib);
local exports = {};
local NetClientFunction;
local _0 = TS.import(script.Parent, "internal");
local getRemoteOrThrow, IS_CLIENT, functionExists, waitForFunction, MAX_CLIENT_WAITFORCHILD_TIMEOUT = _0.getRemoteOrThrow, _0.IS_CLIENT, _0.functionExists, _0.waitForFunction, _0.MAX_CLIENT_WAITFORCHILD_TIMEOUT;
do
NetClientFunction = setmetatable({}, {
__tostring = function() return "NetClientFunction" end;
});
NetClientFunction.__index = NetClientFunction;
function NetClientFunction.new(...)
local self = setmetatable({}, NetClientFunction);
self:constructor(...);
return self;
end;
function NetClientFunction:constructor(name)
self.lastPing = -1;
self.cached = {};
self.instance = getRemoteOrThrow("RemoteFunction", name);
assert(IS_CLIENT, "Cannot create a Net.ClientFunction on the Server!");
assert(functionExists(name), "The specified function '" .. name .. "' does not exist!");
end;
NetClientFunction.WaitFor = TS.async(function(self, name)
local fun = waitForFunction(name, MAX_CLIENT_WAITFORCHILD_TIMEOUT);
if not fun then
error("Failed to retrieve client Function!");
end;
return NetClientFunction.new(name);
end);
function NetClientFunction:getCallback()
return self.instance.OnClientInvoke;
end;
function NetClientFunction:setCallback(func)
self.instance.OnClientInvoke = func;
end;
function NetClientFunction:getInstance()
return self.instance;
end;
function NetClientFunction:getCache()
local cache = self.instance:FindFirstChild("Cache");
if cache then
return cache.Value;
else
return 0;
end;
end;
function NetClientFunction:CallServer(...)
local args = { ... };
if self.lastPing < os.time() + self:getCache() then
local result = self.instance:InvokeServer(unpack(args));
self.cached = result;
self.lastPing = os.time();
return result;
else
return self.cached;
end;
end;
NetClientFunction.CallServerAsync = TS.async(function(self, ...)
local args = { ... };
return self:CallServer(unpack(args));
end);
end;
exports.default = NetClientFunction;
return exports;
27 changes: 27 additions & 0 deletions lualib/GlobalClientEvent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Compiled with https://roblox-ts.github.io v0.2.14
-- August 6, 2019, 5:39 PM New Zealand Standard Time

local TS = require(script.Parent.vendor.RuntimeLib);
local exports = {};
local getGlobalRemoteId = TS.import(script.Parent, "internal").getGlobalRemote;
local NetClientEvent = TS.import(script.Parent, "ClientEvent").default;
local NetGlobalClientEvent;
do
NetGlobalClientEvent = setmetatable({}, {
__tostring = function() return "NetGlobalClientEvent" end;
});
NetGlobalClientEvent.__index = NetGlobalClientEvent;
function NetGlobalClientEvent.new(...)
local self = setmetatable({}, NetGlobalClientEvent);
self:constructor(...);
return self;
end;
function NetGlobalClientEvent:constructor(name)
self.instance = NetClientEvent.new(getGlobalRemoteId(name));
end;
function NetGlobalClientEvent:Connect(callback)
self.instance:Connect(callback);
end;
end;
exports.default = NetGlobalClientEvent;
return exports;
112 changes: 112 additions & 0 deletions lualib/GlobalEvent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
-- Compiled with https://roblox-ts.github.io v0.2.14
-- August 6, 2019, 5:39 PM New Zealand Standard Time

local TS = require(script.Parent.vendor.RuntimeLib);
local exports = {};
local NetGlobalEvent;
local _0 = TS.import(script.Parent, "internal");
local ServerTickFunctions, isLuaTable = _0.ServerTickFunctions, _0.isLuaTable;
local MockMessagingService = TS.import(script.Parent, "MockMessagingService");
local MessagingService = game:GetService("MessagingService");
local Players = game:GetService("Players");
local IS_STUDIO = game:GetService("RunService"):IsStudio();
local function isSubscriptionMessage(value)
if isLuaTable(value) then
local hasData = (value["Data"] ~= nil);
return hasData;
else
return false;
end;
end;
local function isJobTargetMessage(value)
if isSubscriptionMessage(value) then
if isLuaTable(value.Data) then
return (value.Data["jobId"] ~= nil);
end;
end;
return false;
end;
local globalMessageQueue = {};
local lastQueueTick = 0;
local globalEventMessageCounter = 0;
local globalSubscriptionCounter = 0;
local function processMessageQueue()
if tick() >= lastQueueTick + 60 then
globalEventMessageCounter = 0;
globalSubscriptionCounter = 0;
lastQueueTick = tick();
while #globalMessageQueue > 0 do
local _1 = #globalMessageQueue;
local message = globalMessageQueue[_1];
globalMessageQueue[_1] = nil; -- globalMessageQueue.pop
MessagingService:PublishAsync(message.Name, message.Data);
globalEventMessageCounter = globalEventMessageCounter + 1;
end;
if globalEventMessageCounter >= NetGlobalEvent:GetMessageLimit() then
warn("[rbx-net] Too many messages are being sent, any further messages will be queued!");
end;
end;
end;
do
NetGlobalEvent = setmetatable({}, {
__tostring = function() return "NetGlobalEvent" end;
});
NetGlobalEvent.__index = NetGlobalEvent;
function NetGlobalEvent.new(...)
local self = setmetatable({}, NetGlobalEvent);
self:constructor(...);
return self;
end;
function NetGlobalEvent:constructor(name)
self.name = name;
end;
function NetGlobalEvent.GetMessageLimit(self)
return 150 + 60 * #Players:GetPlayers();
end;
function NetGlobalEvent.GetSubscriptionLimit(self)
return 5 + 2 * #Players:GetPlayers();
end;
function NetGlobalEvent:SendToServer(jobId, message)
self:SendToAllServers({
jobId = jobId;
message = message;
});
end;
function NetGlobalEvent:SendToAllServers(message)
local limit = NetGlobalEvent:GetMessageLimit();
if globalEventMessageCounter >= limit then
warn("[rbx-net] Exceeded message limit of " .. tostring(limit) .. ", adding to queue...");
globalMessageQueue[#globalMessageQueue + 1] = {
Name = self.name;
Data = message;
};
else
globalEventMessageCounter = globalEventMessageCounter + 1;
TS.Promise.spawn(function()
((IS_STUDIO and MockMessagingService) or MessagingService):PublishAsync(self.name, message);
end);
end;
end;
function NetGlobalEvent:Connect(handler)
local limit = NetGlobalEvent:GetSubscriptionLimit();
if globalSubscriptionCounter >= limit then
error("[rbx-net] Exceeded Subscription limit of " .. tostring(limit) .. "!");
end;
globalSubscriptionCounter = globalSubscriptionCounter + 1;
return ((IS_STUDIO and MockMessagingService) or MessagingService):SubscribeAsync(self.name, function(recieved)
local Sent = recieved.Sent;
if isJobTargetMessage(recieved) then
local Data = recieved.Data;
if game.JobId == Data.JobId then
handler(Data.InnerData, Sent);
end;
else
handler(recieved.Data, Sent);
end;
end);
end;
end;
ServerTickFunctions[#ServerTickFunctions + 1] = processMessageQueue;
exports.isSubscriptionMessage = isSubscriptionMessage;
exports.default = NetGlobalEvent;
return exports;
119 changes: 119 additions & 0 deletions lualib/GlobalServerEvent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
-- Compiled with https://roblox-ts.github.io v0.2.14
-- August 6, 2019, 5:39 PM New Zealand Standard Time

local TS = require(script.Parent.vendor.RuntimeLib);
local exports = {};
local NetServerEvent = TS.import(script.Parent, "ServerEvent").default;
local _0 = TS.import(script.Parent, "GlobalEvent");
local NetGlobalEvent, isSubscriptionMessage, ISubscriptionMessage = _0.default, _0.isSubscriptionMessage, _0.ISubscriptionMessage;
local _1 = TS.import(script.Parent, "internal");
local getGlobalRemote, IS_CLIENT, isLuaTable = _1.getGlobalRemote, _1.IS_CLIENT, _1.isLuaTable;
local Players = game:GetService("Players");
local function isTargetedSubscriptionMessage(value)
if isSubscriptionMessage(value) then
if isLuaTable(value.Data) then
return (value.Data["InnerData"] ~= nil);
end;
end;
return false;
end;
local NetGlobalServerEvent;
do
NetGlobalServerEvent = setmetatable({}, {
__tostring = function() return "NetGlobalServerEvent" end;
});
NetGlobalServerEvent.__index = NetGlobalServerEvent;
function NetGlobalServerEvent.new(...)
local self = setmetatable({}, NetGlobalServerEvent);
self:constructor(...);
return self;
end;
function NetGlobalServerEvent:constructor(name)
self.instance = NetServerEvent.new(getGlobalRemote(name));
self.event = NetGlobalEvent.new(name);
assert(not IS_CLIENT, "Cannot create a Net.GlobalServerEvent on the Client!");
self.eventHandler = self.event:Connect(function(message)
if isTargetedSubscriptionMessage(message) then
self:recievedMessage(message.Data);
else
warn("[rbx-net] Recieved malformed message for GlobalServerEvent: " .. name);
end;
end);
end;
function NetGlobalServerEvent:getPlayersMatchingId(matching)
if (typeof(matching) == "number") then
return Players:GetPlayerByUserId(matching);
else
local players = {};
for _2 = 1, #matching do
local id = matching[_2];
local player = Players:GetPlayerByUserId(id);
if player then
players[#players + 1] = player;
end;
end;
return players;
end;
end;
function NetGlobalServerEvent:recievedMessage(message)
if message.TargetIds then
local players = self:getPlayersMatchingId(message.TargetIds);
if players then
self.instance:SendToPlayers(players, unpack(message.InnerData));
end;
elseif message.TargetId then
local player = self:getPlayersMatchingId(message.TargetId);
if player then
self.instance:SendToPlayer(player, unpack(message.InnerData));
end;
else
self.instance:SendToAllPlayers(unpack(message.InnerData));
end;
end;
function NetGlobalServerEvent:Disconnect()
self.eventHandler:Disconnect();
end;
function NetGlobalServerEvent:SendToAllServers(...)
local args = { ... };
self.event:SendToAllServers({
data = { unpack(args) };
});
end;
function NetGlobalServerEvent:SendToServer(jobId, ...)
local args = { ... };
self.event:SendToServer(jobId, {
data = { unpack(args) };
});
end;
function NetGlobalServerEvent:SendToPlayer(userId, ...)
local args = { ... };
local player = Players:GetPlayerByUserId(userId);
if player then
self.instance:SendToPlayer(player, unpack(args));
else
self.event:SendToAllServers({
data = { unpack(args) };
targetId = userId;
});
end;
end;
function NetGlobalServerEvent:SendToPlayers(userIds, ...)
local args = { ... };
for _2 = 1, #userIds do
local targetId = userIds[_2];
local player = Players:GetPlayerByUserId(targetId);
if player then
self.instance:SendToPlayer(player, unpack(args));
table.remove(userIds, targetId + 1);
end;
end;
if #userIds > 0 then
self.event:SendToAllServers({
data = { unpack(args) };
targetIds = userIds;
});
end;
end;
end;
exports.default = NetGlobalServerEvent;
return exports;
Loading

0 comments on commit 721dcf8

Please sign in to comment.