Skip to content

Commit

Permalink
Merge pull request #15 from RoStrap/Better-Erroring
Browse files Browse the repository at this point in the history
Error when breaking rules
  • Loading branch information
Validark authored Oct 29, 2018
2 parents 8a113fc + 85c0d4f commit 74bc244
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 43 deletions.
99 changes: 63 additions & 36 deletions PseudoInstance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ local function superinit(self, ...)
self.currentclass = nil
self.superinit = nil
end

CurrentClass.Init(self, ...)
end

Expand Down Expand Up @@ -213,6 +213,53 @@ function PseudoInstance.Register(_, ClassName, ClassData, Superclass)
end
end

ClassData.Init = ClassData.Init or DefaultInit
ClassData.ClassName = ClassName

-- Make properties of internal objects externally accessible
if ClassData.WrappedProperties then
for ObjectName, Properties in next, ClassData.WrappedProperties do
for i = 1, #Properties do
local Property = Properties[i]

if ClassData.Properties[Property] then
Debug.Error("Identifier \"" .. Property .. "\" was used in both Properties and WrappedProperties")
else
ClassData.Properties[Property] = function(this, Value)
local Object = this[ObjectName]

if Object then
Object[Property] = Value
end

this:rawset(Property, Value)
end
end
end
end

local PreviousInit = ClassData.Init

ClassData.Init = function(self, ...)
PreviousInit(self, ...)

for ObjectName, Properties in next, ClassData.WrappedProperties do
for i = 1, #Properties do
local Property = Properties[i]
local Object = self[ObjectName]

if Object then
if self[Property] == nil then
self[Property] = Object[Property] -- This will implicitly error if they do something stupid
end
else
Debug.Error(ObjectName .. " is not a valid member of " .. ClassName)
end
end
end
end
end

if Superclass == nil then
Superclass = Templates.PseudoInstance
end
Expand All @@ -226,50 +273,31 @@ function PseudoInstance.Register(_, ClassName, ClassData, Superclass)
local ClassTable = ClassData[DataTable]
for i, v in next, Superclass[DataTable] do
if not ClassTable[i] then
ClassTable[i] = v == 0 and a == MethodIndex and Debug.Error(ClassName .. " failed to implement " .. i .. " from its superclass " .. Superclass.ClassName) or v
ClassTable[i] = v == 0 and Debug.Error(ClassName .. " failed to implement " .. i .. " from its superclass " .. Superclass.ClassName) or v
end
end
end
else
ClassData.HasSuperclass = false
end

ClassData.Init = ClassData.Init or DefaultInit
ClassData.ClassName = ClassName

-- Make properties of internal objects externally accessible
if ClassData.WrappedProperties then
for ObjectName, Properties in next, ClassData.WrappedProperties do
for i = 1, #Properties do
local Property = Properties[i]

ClassData.Properties[Property] = function(this, Value)
local Object = this[ObjectName]

if Object then
Object[Property] = Value
end
local Identifiers = {} -- Make sure all identifiers are unique

this:rawset(Property, Value)
end
end

local PreviousInit = ClassData.Init
ClassData.Init = function(self, ...)
PreviousInit(self, ...)

for i = 1, #Properties do
local Property = Properties[i]
local Object = self[ObjectName]
if self[Property] == nil and Object and Object[Property] ~= nil then
self[Property] = Object[Property]
end
for a = 1, #DataTableNames do -- Make sure there aren't any duplicate names
local DataTableName = DataTableNames[a]
for i in next, ClassData[DataTableName] do
if type(i) == "string" then
if Identifiers[i] then
Debug.Error("Identifier \"" .. i .. "\" was used in both " .. DataTableNames[Identifiers[i]] .. " and " .. DataTableName)
else
Identifiers[i] = a
end
else
Debug.Error("%q is not a valid Identifier, found inside " .. DataTableName, i)
end
end
end

ClassData.WrappedProperties = nil
local LockedClass = Table.Lock(ClassData)
Templates[ClassName] = LockedClass
return LockedClass
Expand Down Expand Up @@ -440,7 +468,6 @@ PseudoInstance:Register("PseudoInstance", { -- Generates a rigidly defined userd
if self == InternalSelf then
self.Janitor[GlobalSelf] = nil
Metatables[GlobalSelf] = nil
break
end
end

Expand Down Expand Up @@ -486,7 +513,7 @@ function PseudoInstance.new(ClassName, ...)

if not Class then
Resources:LoadLibrary(ClassName)
Class = Templates[ClassName] or Debug.Error("Invalid ClassName")
Class = Templates[ClassName] or Debug.Error("Invalid ClassName: " .. ClassName)
end

if Class.Abstract then
Expand Down Expand Up @@ -529,11 +556,11 @@ function PseudoInstance.new(ClassName, ...)

if rawget(Mt, "currentclass") then
local StoppedOnClass = Class

while StoppedOnClass.HasSuperclass and StoppedOnClass.Superclass ~= Mt.currentclass do
StoppedOnClass = StoppedOnClass.Superclass
end

Debug.Error("Must call self:superinit(...) from " .. StoppedOnClass.ClassName .. ".Init")
end

Expand Down
12 changes: 5 additions & 7 deletions ReplicatedPseudoInstance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")

local IsServer = RunService:IsServer()
local IsClient = RunService:IsClient()
local ReplicateToClients = IsServer and not IsClient -- Don't Replicate in SoloTestMode
local ReplicateToServer = not IsServer and IsClient

local Resources = require(ReplicatedStorage:WaitForChild("Resources"))
local Debug = Resources:LoadLibrary("Debug")
Expand Down Expand Up @@ -179,7 +177,7 @@ local function OnPropertyChanged(self, i)
end
end

if ReplicateToClients then
if IsServer then
Players.PlayerAdded:Connect(function(Player)
if RemoteFunction:InvokeClient(Player) then -- Yield until player loads
local NumReplicationOrder = #ReplicationOrder
Expand All @@ -202,7 +200,7 @@ if ReplicateToClients then

Event:Fire(Player, select(2, ...))
end)
elseif ReplicateToServer then
elseif IsClient then
local OnClientEventNumber = 1 -- Guarenteed that this will resolve in the order in which replication is intended to occur

RemoteEvent.OnClientEvent:Connect(function(EventNumber, ClassName, Id, RawData, Assigned) -- Handle objects being replicated to clients
Expand Down Expand Up @@ -261,7 +259,7 @@ return PseudoInstance:Register("ReplicatedPseudoInstance", {
self.__class.Storage[Id] = nil
ReplicationOrder:RemoveElement(Id)

if ReplicateToClients then -- Replicate Destroy
if IsServer then -- Replicate Destroy
ReplicateUpdateToInterestedParties(self, Id)
end

Expand All @@ -275,13 +273,13 @@ return PseudoInstance:Register("ReplicatedPseudoInstance", {
Init = function(self, Id)
self:superinit()

if ReplicateToClients then
if IsServer then
if not Id then
Id = Ids + 1
Ids = Id
end
self.Changed:Connect(OnPropertyChanged, self)
elseif ReplicateToServer then
elseif IsClient then
if Id then
for Event in next, self.__class.Events do
if Event ~= "Changed" then
Expand Down

0 comments on commit 74bc244

Please sign in to comment.