Initial commit

This commit is contained in:
Nikita Kruglickiy
2020-12-25 22:18:23 +06:00
commit 1a0785862d
21 changed files with 1181 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
BaseEntity = class("BaseEntity")
function BaseEntity:initialize()
end

View File

@@ -0,0 +1,153 @@
local MODEL_PLACEHOLDER = "models/holograms/cube.mdl"
local BUNDLE_SIZE = 30
local SEND_DELAY = 2
MeshBuilder = class("MeshBuilder")
function MeshBuilder:initialize()
self._objects = {}
self._bundle = {}
if CLIENT then
self._meshData = {}
net.receive("sendHolograms", function(len)
local hasNext = net.readBit()
while hasNext == 1 do
local key = net.readString()
net.readEntity(function(ent)
if not self._objects[key] then
local holo = ent:toHologram()
if self._meshData[key] then
holo:setMesh(self._meshData[key])
holo:setRenderBounds(Vector(-200), Vector(200))
end
self._objects[key] = holo
end
end)
hasNext = net.readBit()
end
end)
else
net.receive("sendObjects", function(len, ply)
local hasNext = net.readBit()
while hasNext == 1 do
local key = net.readString()
local pos = net.readVector()
local ang = net.readAngle()
local scale = net.readVector()
local color = net.readColor()
local mat = net.readString()
local parent = net.readEntity()
if not self._objects[key] then
if isValid(parent) then
pos = parent:localToWorld(pos)
ang = parent:localToWorldAngles(ang)
end
local holo = holograms.create(pos, ang, MODEL_PLACEHOLDER, scale)
holo:setColor(color)
holo:setMaterial(mat)
holo:setParent(parent)
self._objects[key] = holo
end
if not self._bundle[key] then
self._bundle[key] = self._objects[key]
end
hasNext = net.readBit()
end
net.start("sendHolograms")
for k, v in pairs(self._bundle) do
if isValid(v) and type(v) == "Hologram" then
net.writeBit(1)
net.writeString(k)
net.writeEntity(v)
end
end
net.writeBit(0)
net.send(ply)
table.empty(self._bundle)
end)
end
end
if CLIENT then
function MeshBuilder:setMeshData(meshData)
self._meshData = meshData
end
function MeshBuilder:setup(key, pos, ang, scale, color, mat, parent)
if not self._objects[key] and not self._bundle[key] and self._meshData[key] then
self._bundle[key] = {
pos = pos,
ang = ang,
scale = scale,
color = color,
mat = mat,
parent = isValid(parent) and parent or chip()
}
end
end
function MeshBuilder:setupAll(pos, ang, scale, color, mat, parent)
for _, v in pairs(table.getKeys(self._meshData)) do
self:setup(v, pos, ang, scale, color, mat, parent)
end
end
function MeshBuilder:build()
local function send(objects)
net.start("sendObjects")
for k, v in pairs(objects) do
net.writeBit(1)
net.writeString(k)
net.writeVector(v.pos)
net.writeAngle(v.ang)
net.writeVector(v.scale)
net.writeColor(v.color)
net.writeString(v.mat)
net.writeEntity(v.parent)
end
net.writeBit(0)
net.send()
end
local bundleKeys = table.getKeys(self._bundle)
if #bundleKeys > BUNDLE_SIZE then
local i = 1
timer.create("send", SEND_DELAY, math.ceil(#bundleKeys / BUNDLE_SIZE), function()
local from = (i - 1) * BUNDLE_SIZE + 1
local objects = {}
for _, v in pairs({ unpack(bundleKeys, from, from + BUNDLE_SIZE - 1) }) do
objects[v] = self._bundle[v]
end
send(objects)
if i < math.ceil(#bundleKeys / BUNDLE_SIZE) then
i = i + 1
else
table.empty(self._bundle)
end
end)
else
send(self._bundle)
end
end
end

View File

@@ -0,0 +1,36 @@
local MAX_QUOTA = 0.75
Parser = class("Parser")
function Parser:initialize(link)
if CLIENT then
local triangles = mesh.trianglesLeft()
local createFromObjCoroutine = coroutine.create(function(objData)
self:OnLoaded(objData, mesh.createFromObj(objData, true))
end)
print("Getting file...")
http.get(link, function(objData)
local loadMesh = coroutine.wrap(function()
self.meshData = mesh.createFromObj(objData, true)
return true
end)
print("File received, start parsing...")
hook.add("think", "loadingMesh", function()
while quotaAverage() < quotaMax() * MAX_QUOTA do
if loadMesh() then
self:onLoaded(objData, self.meshData, triangles - mesh.trianglesLeft())
hook.remove("think", "loadingMesh")
return
end
end
end)
end)
end
end
function Parser:onLoaded(objData, meshData, usedTriangles)
end