Compare commits
46 Commits
a94b95afc8
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c1ec8078b | |||
| d4a70b7882 | |||
| 92d6637695 | |||
| 84bab9bbc5 | |||
| 74997b1e23 | |||
| 6454d741d4 | |||
| 7d63d60d99 | |||
| 9f37c2bd8b | |||
| 083e7dbe7d | |||
| 5789e629f9 | |||
| 6c13722fc6 | |||
| c3a27b2ee9 | |||
| cac6c7e7b8 | |||
| eb211da457 | |||
| 7caa6f8ff5 | |||
| 478a97d2ed | |||
| 6c537591e8 | |||
| ee04bb87dd | |||
| 612a3d0a09 | |||
| 3273054766 | |||
| beabf76562 | |||
| c1aa2837ca | |||
| c320c4bb19 | |||
| 4c8c26bb33 | |||
| a133c5ee94 | |||
| 4895d7cfa5 | |||
| 052ce82588 | |||
| 211cba61a6 | |||
| 4ab646015d | |||
| 9fee67d231 | |||
| 860b73a8f4 | |||
| da38631584 | |||
| 6839203dc5 | |||
| d9ac74101f | |||
| e20400654c | |||
| 058cbed20f | |||
| e0c10318e5 | |||
| 7d609b126d | |||
| 36ecaea919 | |||
| fac01a2849 | |||
| c71c1a98e4 | |||
| f6d5f142e9 | |||
| 70961b0336 | |||
| 9c50274f29 | |||
| cee36635e0 | |||
| d0337f5dae |
@ -130,6 +130,11 @@ extern class Krom {
|
||||
static function readStorage(name: String): haxe.io.BytesData;
|
||||
|
||||
static function fileSaveBytes(path: String, bytes: haxe.io.BytesData): Void;
|
||||
static function fileWriteBytes(path: String, bytes: haxe.io.BytesData, ?offset: Float, ?byteLength: Int): Void;
|
||||
static function fileReadBytes(path: String, ?offset: Float, ?length: Int): haxe.io.BytesData;
|
||||
static function fileSize(path: String): Float;
|
||||
static function fileExists(path: String): Bool;
|
||||
static function deleteFile(path: String): Void;
|
||||
static function sysCommand(cmd: String, ?args: Array<String>): Int;
|
||||
static function savePath(): String;
|
||||
static function getArgCount(): Int;
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Krom/Krom.exe
BIN
Krom/Krom.exe
Binary file not shown.
Binary file not shown.
@ -1,7 +0,0 @@
|
||||
# The zlib/libpng License
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
This notice may not be removed or altered from any source distribution.
|
||||
@ -1,7 +0,0 @@
|
||||
* 2024-12-02: Submodules have been moved in to this repository (previously contained in https://github.com/leenkx3d/lnxsdk).
|
||||
* 2019-06-30: Return value of `PhysicsWorld.rayCast()` changed, see https://github.com/leenkx3d/leenkx/commit/dfb7609a28cebf3a520e6a25a8563d01c32f2b01.
|
||||
* 2019-04-06: Use voxelao instead of voxelgi, gi will be reworked into raytracing.
|
||||
* 2019-01-13: If you are using Leenkx Updater, get Leenkx 0.6beta from https://leenkx.itch.io/leenkx3d first (or clone the sdk from https://github.com/leenkx3d/lnxsdk).
|
||||
* 2018-08-28: `LampObject` and `LampData` is now `LightObject` and `LightData`
|
||||
* 2018-06-01: 'Not Equal' has been removed from the Gate logic node. Use 'Equal' and 'False' output socket instead.
|
||||
* 2017-11-20: Use `leenkx.trait.physics.*` instead of `leenkx.trait.internal.*` to access physics traits.
|
||||
@ -6,7 +6,7 @@ bl_info = {
|
||||
"location": "Properties -> Render -> Leenkx Player",
|
||||
"description": "Full Stack SDK",
|
||||
"author": "Leenkx.com",
|
||||
"version": (2026, 9, 0),
|
||||
"version": (2026, 10, 6),
|
||||
"blender": (5, 2, 0),
|
||||
"doc_url": "https://leenkx.com/",
|
||||
"tracker_url": "https://leenkx.com/support"
|
||||
@ -361,7 +361,7 @@ class LeenkxAddonPreferences(AddonPreferences):
|
||||
layout.label(text="Welcome to Leenkx!")
|
||||
|
||||
# Compare version Blender and Leenkx (major, minor)
|
||||
if bpy.app.version[:2] not in [(4, 5), (4, 4), (4, 2), (3, 6), (3, 3)]:
|
||||
if bpy.app.version[:2] not in [(5, 2), (4, 5), (4, 4), (4, 2), (3, 6), (3, 3)]:
|
||||
box = layout.box().column()
|
||||
box.label(text="Warning: For Leenkx to work correctly, use a Blender LTS version")
|
||||
|
||||
|
||||
@ -593,7 +593,7 @@ fragColor.rgb = min(fragColor.rgb, 65504 * 0.5);
|
||||
#endif
|
||||
#ifdef _CToneFilmic2
|
||||
fragColor.rgb = acesFilm(fragColor.rgb);
|
||||
fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 1.0));
|
||||
fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));
|
||||
#endif
|
||||
#ifdef _CToneReinhard
|
||||
fragColor.rgb = tonemapReinhard(fragColor.rgb);
|
||||
|
||||
@ -3,8 +3,8 @@
|
||||
{
|
||||
"name": "probe_cubemap",
|
||||
"depth_write": false,
|
||||
"compare_mode": "less",
|
||||
"cull_mode": "clockwise",
|
||||
"compare_mode": "greater",
|
||||
"cull_mode": "counter_clockwise",
|
||||
"blend_source": "blend_one",
|
||||
"blend_destination": "blend_one",
|
||||
"blend_operation": "add",
|
||||
|
||||
@ -3,8 +3,8 @@
|
||||
{
|
||||
"name": "probe_planar",
|
||||
"depth_write": false,
|
||||
"compare_mode": "less",
|
||||
"cull_mode": "clockwise",
|
||||
"compare_mode": "greater",
|
||||
"cull_mode": "counter_clockwise",
|
||||
"blend_source": "blend_one",
|
||||
"blend_destination": "blend_one",
|
||||
"blend_operation": "add",
|
||||
|
||||
@ -81,11 +81,11 @@ void main() {
|
||||
vec4 gr = textureLod(gbuffer_refraction, texCoord, 0.0);
|
||||
float ior = unpackIOR(gr.x);
|
||||
float transmittance = gr.y;
|
||||
float surfaceDepth = gr.z;
|
||||
float surfaceDepth = textureLod(gbufferD, texCoord, 0.0).r;
|
||||
float d = surfaceDepth * 2.0 - 1.0;
|
||||
|
||||
vec4 sceneSample = textureLod(tex, texCoord, 0.0);
|
||||
if (surfaceDepth == 0.0 || surfaceDepth == 1.0) {
|
||||
if (gr.z == 0.0 || surfaceDepth == 1.0) {
|
||||
fragColor = sceneSample;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -96,41 +96,81 @@ vec3 sampleWaterNormals(vec2 hitXY, float speed, out vec2 tcnor0, out vec2 tcnor
|
||||
return normalize(n0 + n1 - 1.0);
|
||||
}
|
||||
|
||||
float waveHeight(vec2 xy, float speed) {
|
||||
return (sin(xy.x * 10.0 / waterDisplace + speed) * cos(xy.y * 10.0 / waterDisplace + speed) + sin(xy.x * 20.0 / waterDisplace + speed * 1.3) * cos(xy.y * 20.0 / waterDisplace + speed * 1.3) * 0.5) / 50.0 * waterDisplace;
|
||||
}
|
||||
|
||||
void main() {
|
||||
float gdepth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
|
||||
vec3 vray = normalize(viewRay);
|
||||
float speed = time * 2.0 * waterSpeed;
|
||||
bool isSky = (gdepth == 1.0);
|
||||
float farPlane = cameraProj.y / (1.0 - cameraProj.x);
|
||||
float cosAng = dot(eyeLook, vray);
|
||||
float domeR = farPlane * 0.94;
|
||||
float tDome = -3.5 * vray.z + sqrt(max(domeR * domeR - 12.25 * (1.0 - vray.z * vray.z), 0.0));
|
||||
float domeDepth01 = cameraProj.x + cameraProj.y / (tDome * cosAng);
|
||||
bool isSky = (gdepth * 0.5 + 0.5) >= domeDepth01;
|
||||
|
||||
// Ray-plane intersection with water surface (z = waterLevel)
|
||||
float denom = dot(vray, vec3(0.0, 0.0, 1.0));
|
||||
float denom = vray.z;
|
||||
float tWater = (waterLevel - eye.z) / denom;
|
||||
bool hasWaterHit = (abs(denom) > 0.0001) && (tWater > 0.0);
|
||||
|
||||
if (eye.z < waterLevel) {
|
||||
vec2 tc = texCoord;
|
||||
float fogFactor;
|
||||
|
||||
if (hasWaterHit && denom > 0.0) {
|
||||
// Looking up at water surface - apply normal distortion
|
||||
vec3 hit = eye + tWater * vray;
|
||||
vec2 tc0, tc1;
|
||||
vec3 n2 = sampleWaterNormals(hit.xy * waterFreq, speed, tc0, tc1);
|
||||
tc = texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
|
||||
fogFactor = clamp(tWater * waterDensity, 0.0, 0.95);
|
||||
} else {
|
||||
// Looking forward/down - distort via water surface above fragment
|
||||
vec3 p = getPos(eye, eyeLook, vray, gdepth, cameraProj);
|
||||
vec2 wxy = isSky ? (eye.xy + vray.xy * 50.0) : p.xy;
|
||||
vec2 tc0, tc1;
|
||||
vec3 n2 = sampleWaterNormals(wxy * waterFreq, speed, tc0, tc1);
|
||||
tc = texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
|
||||
float waterDist = isSky ? 50.0 : length(p - eye);
|
||||
fogFactor = clamp(waterDist * waterDensity, 0.0, 0.95);
|
||||
float depthThick = 1.0 + (waterLevel - eye.z);
|
||||
vec3 p = vec3(0.0);
|
||||
float geomDist = farPlane;
|
||||
if (!isSky) {
|
||||
p = getPos(eye, eyeLook, vray, gdepth, cameraProj);
|
||||
geomDist = length(p - eye);
|
||||
}
|
||||
bool upRay = hasWaterHit && denom > 0.0;
|
||||
float waterDist = upRay ? min(tWater, geomDist) : geomDist;
|
||||
|
||||
vec2 wxy;
|
||||
if (upRay && tWater < geomDist) {
|
||||
wxy = (eye + tWater * vray).xy;
|
||||
} else if (!isSky) {
|
||||
wxy = p.xy;
|
||||
} else {
|
||||
wxy = eye.xy + vray.xy * 50.0;
|
||||
}
|
||||
vec2 tc0, tc1;
|
||||
vec3 n2 = sampleWaterNormals(wxy * waterFreq, speed, tc0, tc1);
|
||||
tc = texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
|
||||
float ddepth2 = textureLod(gbufferD, tc, 0.0).r * 2.0 - 1.0;
|
||||
float dist2 = cameraProj.y / ((ddepth2 * 0.5 + 0.5) - cameraProj.x) / cosAng;
|
||||
if (dist2 > geomDist + 1.0) tc = texCoord;
|
||||
|
||||
vec3 refracted = textureLod(tex, tc, 0.0).rgb;
|
||||
fragColor.rgb = mix(refracted, waterColor, fogFactor);
|
||||
vec3 outCol = refracted;
|
||||
|
||||
if (upRay && tWater < geomDist) {
|
||||
outCol *= max(1.0 - length(n2.xy) * 1.5, 0.0);
|
||||
vec3 h2 = normalize(-vray + ld);
|
||||
outCol += pow(max(dot(n2, h2), 0.0), 80.0) * 0.6;
|
||||
}
|
||||
|
||||
if (!isSky) {
|
||||
float fdw = abs(p.z - (waterLevel + waveHeight(p.xy, speed)));
|
||||
float bandw = 0.1 + waterDist * 0.003;
|
||||
if (fdw < bandw) {
|
||||
float foamLod = clamp(waterDist / 15.0, 0.0, 5.0);
|
||||
vec3 fm0 = textureLod(sfoam, tc0 * 3.0 + vec2(speed / 30.0, speed / 50.0), foamLod).rgb;
|
||||
vec3 fm1 = textureLod(sfoam, tc1 * 4.0 + vec2(-speed / 35.0, speed / 45.0), foamLod).rgb;
|
||||
float fstr = clamp(1.0 - fm0.r * 0.5 - fm1.b * 0.5, 0.0, 1.0);
|
||||
float ffac = clamp(1.0 - fdw / bandw, 0.0, 1.0);
|
||||
float foamFade = clamp(1.0 - (waterLevel - eye.z) * 0.4, 0.0, 1.0);
|
||||
vec3 foamCol = mix(waterColor, vec3(1.0), 0.6);
|
||||
outCol = mix(outCol, foamCol, ffac * fstr * 0.5 * foamFade);
|
||||
}
|
||||
}
|
||||
|
||||
float fogFactor = 1.0 - exp(-waterDist * waterDensity * 0.006 * depthThick);
|
||||
outCol = mix(outCol, waterColor, clamp(fogFactor, 0.0, 1.0));
|
||||
|
||||
fragColor.rgb = outCol;
|
||||
fragColor.a = 1.0;
|
||||
return;
|
||||
}
|
||||
@ -144,21 +184,19 @@ void main() {
|
||||
if (isSky) tWater = min(tWater, 100.0); // Clamp to prevent aliasing at horizon
|
||||
vec3 p = isSky ? (eye + tWater * vray) : getPos(eye, eyeLook, vray, gdepth, cameraProj);
|
||||
|
||||
float horizonFactor = clamp(1.0 - tWater / 60.0, 0.0, 1.0);
|
||||
if (!isSky && p.z > waterLevel) {
|
||||
fragColor = vec4(0.0);
|
||||
if (!isSky && p.z > waterLevel - 0.01) {
|
||||
fragColor = textureLod(tex, texCoord, 0.0);
|
||||
return;
|
||||
}
|
||||
float horizonFactor = clamp(1.0 - tWater / 60.0, 0.0, 1.0);
|
||||
|
||||
// Displace surface
|
||||
float geomZ = p.z; // undisplaced depth for foam/fog tests
|
||||
p.z += (sin(p.x * 10.0 / waterDisplace + speed) * cos(p.y * 10.0 / waterDisplace + speed)
|
||||
+ sin(p.x * 20.0 / waterDisplace + speed * 1.3) * cos(p.y * 20.0 / waterDisplace + speed * 1.3) * 0.5)
|
||||
/ 50.0 * waterDisplace;
|
||||
p.z += waveHeight(p.xy, speed);
|
||||
|
||||
// Hit plane to determine uvs
|
||||
vec3 v = normalize(eye - p);
|
||||
float t = (waterLevel - eye.z) / dot(v, vec3(0.0, 0.0, 1.0));
|
||||
float t = (waterLevel - eye.z) / v.z;
|
||||
vec3 hit = eye + t * v;
|
||||
|
||||
// Sample normal maps
|
||||
@ -171,8 +209,8 @@ void main() {
|
||||
tc = texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
|
||||
} else {
|
||||
float ddepth = textureLod(gbufferD, texCoord + (n2.xy * n2.z) / 40.0, 0.0).r * 2.0 - 1.0;
|
||||
vec3 p2 = getPos(eye, eyeLook, vray, ddepth, cameraProj);
|
||||
tc = p2.z > waterLevel ? texCoord : texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
|
||||
float dist2 = cameraProj.y / ((ddepth * 0.5 + 0.5) - cameraProj.x) / cosAng;
|
||||
tc = (eye.z + vray.z * dist2) > waterLevel ? texCoord : texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
|
||||
}
|
||||
|
||||
// Light
|
||||
@ -222,26 +260,25 @@ void main() {
|
||||
#else
|
||||
fragColor.rgb = mix(refracted, reflectedEnv, waterReflect * fresnel);
|
||||
#endif
|
||||
// Water color tint - blend rather than multiply to preserve brightness
|
||||
float colorMix = isSky ? 0.7 : 0.5;
|
||||
fragColor.rgb = mix(fragColor.rgb, fragColor.rgb * waterColor, colorMix * horizonFactor);
|
||||
fragColor.rgb = mix(fragColor.rgb, waterColor, colorMix);
|
||||
// Blinn-Phong specular using half-vector, faded at horizon
|
||||
vec3 h = normalize(v + ld);
|
||||
float specAmount = pow(max(dot(n2, h), 0.0), 200.0) * (200.0 + 8.0) / (PI * 8.0);
|
||||
fragColor.rgb += specAmount * (isSky ? 0.3 : 1.0);
|
||||
// Depth fog - blend toward waterColor with depth, faded at horizon
|
||||
float depthFog = clamp(-(geomZ - waterLevel) * waterDensity, 0.0, 0.9);
|
||||
fragColor.rgb = mix(fragColor.rgb, waterColor, depthFog * horizonFactor);
|
||||
fragColor.rgb += specAmount * (isSky ? 0.3 : 1.0) * horizonFactor;
|
||||
float waterColumn = isSky ? 1.0 : max(length(p - eye) - tWater, 0.0);
|
||||
float depthFog = 1.0 - exp(-waterColumn * waterDensity * 0.006);
|
||||
vec3 surfaceCol = mix(waterColor, reflectedEnv, waterReflect * fresnel);
|
||||
fragColor.rgb = mix(fragColor.rgb, surfaceCol, min(depthFog, 0.9));
|
||||
// Alpha fades smoothly at horizon instead of hard cut
|
||||
// Skydome sits 3.5 below the camera (_skydomeMatrix), so its horizon
|
||||
// appears where the ray z reaches -3.5 / domeRadius
|
||||
float farPlane = cameraProj.y / (1.0 - cameraProj.x);
|
||||
float horizonFade = clamp((-3.5 / (farPlane * 0.95) - vray.z) * 20.0, 0.0, 1.0);
|
||||
fragColor.a = isSky ? horizonFade : clamp(abs(geomZ - waterLevel) * 5.0, 0.0, 1.0);
|
||||
|
||||
// Foam - based on actual geometry depth below water surface
|
||||
float fd = isSky ? 1.0 : abs(geomZ - waterLevel);
|
||||
if (fd < 0.1) {
|
||||
fragColor.a = isSky ? horizonFade : clamp(abs(geomZ - waterLevel) * 5.0 + 0.1, 0.0, 1.0);
|
||||
float waveH = p.z - geomZ; // displacement already applied above
|
||||
float band = 0.1 + tWater * 0.003;
|
||||
float fd = isSky ? 1.0 : abs(geomZ - (waterLevel + waveH));
|
||||
if (fd < band) {
|
||||
// Based on foam by Owen Deery
|
||||
// http://fire-face.com/personal/water
|
||||
// Distance-based LOD blurs foam at range to reduce noise
|
||||
@ -250,8 +287,10 @@ void main() {
|
||||
vec2 foamUV1 = tcnor1 * 4.0 + vec2(-speed / 35.0, speed / 45.0);
|
||||
vec3 foamMask0 = textureLod(sfoam, foamUV0, foamLod).rgb;
|
||||
vec3 foamMask1 = textureLod(sfoam, foamUV1, foamLod).rgb;
|
||||
float foamStrength = clamp(1.0 - foamMask0.r * 0.5 - foamMask1.b * 0.5, 0.0, 1.0);
|
||||
float fac = (1.0 - (fd * (1.0 / 0.1))) * horizonFactor;
|
||||
fragColor.rgb = mix(fragColor.rgb, mix(fragColor.rgb, waterColor + 0.2, foamStrength), clamp(fac, 0.0, 1.0) * 0.5);
|
||||
float foamMask = clamp(1.0 - foamMask0.r * 0.5 - foamMask1.b * 0.5, 0.0, 1.0);
|
||||
float fac = clamp(1.0 - fd / band, 0.0, 1.0);
|
||||
vec3 foamCol = mix(waterColor, vec3(1.0), 0.6);
|
||||
fragColor.rgb = mix(fragColor.rgb, foamCol, fac * foamMask * 0.8);
|
||||
fragColor.a = max(fragColor.a, fac * foamMask);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,13 @@ import iron.object.LightObject;
|
||||
import iron.object.MeshObject;
|
||||
import iron.object.Uniforms;
|
||||
import iron.object.Clipmap;
|
||||
#if lnx_radix
|
||||
import haxe.io.FPHelper;
|
||||
import radix.Radix;
|
||||
#end
|
||||
#if lnx_occlusion_culling
|
||||
import iron.culling.CullSystem;
|
||||
#end
|
||||
#if lnx_vr
|
||||
import iron.math.Vec4;
|
||||
import iron.math.Mat4;
|
||||
@ -27,17 +34,19 @@ import iron.math.Quat;
|
||||
class RenderPath {
|
||||
public static var active: RenderPath;
|
||||
#if lnx_vr
|
||||
static var vrSimulateMode: Bool = false;
|
||||
static var vrCameraOffsetSet:Bool = false;
|
||||
static var vrCameraOffset:Vec4 = new Vec4();
|
||||
static var wasVRPresenting:Bool = false;
|
||||
public static var vrCalibrationPosition:Vec4 = null;
|
||||
public static var vrCalibrationRotation:iron.math.Quat = null;
|
||||
public static var vrCalibrationSaved:Bool = false;
|
||||
|
||||
|
||||
public static var vrCenterCameraWorld:Mat4 = null;
|
||||
|
||||
|
||||
static var vrOriginalSuperSample:Float = -1.0;
|
||||
#end
|
||||
|
||||
static var vrSimulateMode: Bool = false;
|
||||
public static inline function isVRPresenting(): Bool {
|
||||
#if (kha_webgl && lnx_vr)
|
||||
return kha.vr.VrInterface.instance != null && kha.vr.VrInterface.instance.IsPresenting();
|
||||
@ -45,17 +54,11 @@ class RenderPath {
|
||||
return false;
|
||||
#end
|
||||
}
|
||||
|
||||
|
||||
public static inline function isVRSimulateMode(): Bool {
|
||||
return vrSimulateMode;
|
||||
}
|
||||
|
||||
// TODO: done remove safely
|
||||
public static inline function debugLog(msg: String, once: Bool = true): Void {
|
||||
return;
|
||||
}
|
||||
#end
|
||||
|
||||
public var frameScissor = false;
|
||||
public var frameScissorX = 0;
|
||||
public var frameScissorY = 0;
|
||||
@ -81,6 +84,18 @@ class RenderPath {
|
||||
var renderToXRFramebuffer = false;
|
||||
#end
|
||||
public var drawOrder = DrawOrder.Distance;
|
||||
#if lnx_frustum_culling
|
||||
public var frustumCulling = true;
|
||||
#else
|
||||
public var frustumCulling = false;
|
||||
#end
|
||||
|
||||
#if lnx_occlusion_culling
|
||||
public var occlusionCulling = true;
|
||||
#else
|
||||
public var occlusionCulling = false;
|
||||
#end
|
||||
|
||||
public var paused = false;
|
||||
public var ready(get, never): Bool;
|
||||
function get_ready(): Bool { return loading == 0; }
|
||||
@ -95,6 +110,8 @@ class RenderPath {
|
||||
var lastH = 0;
|
||||
var bindParams: Array<String>;
|
||||
var meshesSorted: Bool;
|
||||
var cullComputed: Bool;
|
||||
var cullFlagsReset: Bool;
|
||||
var scissorSet = false;
|
||||
var viewportScaled = false;
|
||||
var lastFrameTime = 0.0;
|
||||
@ -162,24 +179,24 @@ class RenderPath {
|
||||
|
||||
var appW = iron.App.w();
|
||||
var appH = iron.App.h();
|
||||
|
||||
|
||||
// use native XR framebuffer dimensions
|
||||
#if (kha_webgl && lnx_vr)
|
||||
if (kha.vr.VrInterface.instance != null) {
|
||||
var vr = kha.vr.VrInterface.instance;
|
||||
var isPresenting = vr != null && vr.IsPresenting();
|
||||
|
||||
|
||||
// save/restore camera position between modes
|
||||
if (!wasVRPresenting && isPresenting) {
|
||||
if (Scene.active != null && Scene.active.camera != null) {
|
||||
if (vrCalibrationPosition == null) vrCalibrationPosition = new Vec4();
|
||||
if (vrCalibrationRotation == null) vrCalibrationRotation = new Quat();
|
||||
|
||||
|
||||
vrCalibrationPosition.setFrom(Scene.active.camera.transform.loc);
|
||||
vrCalibrationRotation.setFrom(Scene.active.camera.transform.rot);
|
||||
vrCalibrationSaved = true;
|
||||
}
|
||||
|
||||
|
||||
// save original super sampling for later
|
||||
vrOriginalSuperSample = leenkx.renderpath.Inc.superSample;
|
||||
|
||||
@ -196,18 +213,18 @@ class RenderPath {
|
||||
#if (kha_webgl && lnx_vr)
|
||||
iron.system.Time.vrFrameTime = -1.0;
|
||||
#end
|
||||
|
||||
|
||||
if (vrCalibrationSaved && Scene.active != null && Scene.active.camera != null) {
|
||||
Scene.active.camera.transform.loc.setFrom(vrCalibrationPosition);
|
||||
Scene.active.camera.transform.rot.setFrom(vrCalibrationRotation);
|
||||
Scene.active.camera.buildMatrix();
|
||||
Scene.active.camera.buildProjection();
|
||||
}
|
||||
|
||||
|
||||
// restore original super sampling from simulate mode
|
||||
if (vrOriginalSuperSample >= 0.0) {
|
||||
leenkx.renderpath.Inc.superSample = vrOriginalSuperSample;
|
||||
|
||||
|
||||
for (rt in renderTargets) {
|
||||
if (rt.raw.width == 0 && rt.raw.scale != null) {
|
||||
rt.raw.scale = vrOriginalSuperSample;
|
||||
@ -216,13 +233,13 @@ class RenderPath {
|
||||
resize();
|
||||
vrOriginalSuperSample = -1.0;
|
||||
}
|
||||
|
||||
|
||||
// reset offset for next session
|
||||
vrCameraOffsetSet = false;
|
||||
vrCameraOffset = null;
|
||||
}
|
||||
wasVRPresenting = isPresenting;
|
||||
|
||||
|
||||
if (isPresenting) {
|
||||
// TODO: re-investigate using super sampling to avoid pixelation in simulate mode while giving max quality in headset
|
||||
if (vrOriginalSuperSample >= 0.0 && leenkx.renderpath.Inc.superSample != 4.0) {
|
||||
@ -234,7 +251,7 @@ class RenderPath {
|
||||
}
|
||||
resize();
|
||||
}
|
||||
|
||||
|
||||
var xrVr: kha.js.vr.VrInterface = cast vr;
|
||||
if (xrVr.xrGLLayer != null) {
|
||||
appW = xrVr.xrGLLayer.framebufferWidth;
|
||||
@ -304,6 +321,8 @@ class RenderPath {
|
||||
currentD = 1;
|
||||
currentFace = -1;
|
||||
meshesSorted = false;
|
||||
cullComputed = false;
|
||||
cullFlagsReset = false;
|
||||
sun = null;
|
||||
|
||||
for (l in Scene.active.lights) {
|
||||
@ -384,7 +403,7 @@ class RenderPath {
|
||||
if (currentG != null) end();
|
||||
currentG = g;
|
||||
additionalTargets = additionalRenderTargets;
|
||||
|
||||
|
||||
// we still bind but skip begin() when explicitly rendering to XR framebuffer (renderToXRFramebuffer flag)
|
||||
#if lnx_vr
|
||||
if (!renderToXRFramebuffer) {
|
||||
@ -405,7 +424,7 @@ class RenderPath {
|
||||
currentG.disableScissor();
|
||||
scissorSet = false;
|
||||
}
|
||||
|
||||
|
||||
#if lnx_vr
|
||||
if (beginCalled) {
|
||||
currentG.end();
|
||||
@ -470,6 +489,60 @@ class RenderPath {
|
||||
rt.image.generateMipmaps(1000);
|
||||
}
|
||||
|
||||
#if lnx_radix
|
||||
static var sortDistanceKeys = new Array<Int>();
|
||||
static var sortDistanceMeshesBuf = new Array<MeshObject>();
|
||||
static var sortDistanceKeysBuf = new Array<Int>();
|
||||
|
||||
static var sortIndexKeys = new Array<Int>();
|
||||
static var sortIndexMeshesBuf = new Array<MeshObject>();
|
||||
static var sortIndexKeysBuf = new Array<Int>();
|
||||
|
||||
public static function sortMeshesDistance(meshes: Array<MeshObject>) {
|
||||
var n = meshes.length;
|
||||
var keys = sortDistanceKeys;
|
||||
var meshesBuf = sortDistanceMeshesBuf;
|
||||
var keysBuf = sortDistanceKeysBuf;
|
||||
if (keys.length < n) {
|
||||
keys.resize(n);
|
||||
meshesBuf.resize(n);
|
||||
keysBuf.resize(n);
|
||||
}
|
||||
for (i in 0...n) {
|
||||
var m = meshes[i];
|
||||
var key = FPHelper.floatToI32(m.cameraDistance) >>> 1;
|
||||
#if rp_depth_texture
|
||||
if (m.depthRead) key |= 0x80000000;
|
||||
#end
|
||||
keys[i] = key;
|
||||
}
|
||||
Radix.sortCopyKeyed(meshes, keys, meshesBuf, keysBuf);
|
||||
if (meshesBuf.length > n) meshesBuf.resize(n);
|
||||
}
|
||||
|
||||
public static function sortMeshesIndex(meshes: Array<MeshObject>) {
|
||||
var n = meshes.length;
|
||||
var keys = sortIndexKeys;
|
||||
var meshesBuf = sortIndexMeshesBuf;
|
||||
var keysBuf = sortIndexKeysBuf;
|
||||
if (keys.length < n) {
|
||||
keys.resize(n);
|
||||
meshesBuf.resize(n);
|
||||
keysBuf.resize(n);
|
||||
}
|
||||
for (i in 0...n) {
|
||||
var m = meshes[i];
|
||||
var si = m.data.sortingIndex;
|
||||
var key = si < -0x40000000 ? 0 : si > 0x3fffffff ? 0x7fffffff : si + 0x40000000;
|
||||
#if rp_depth_texture
|
||||
if (m.depthRead) key |= 0x80000000;
|
||||
#end
|
||||
keys[i] = key;
|
||||
}
|
||||
Radix.sortCopyKeyed(meshes, keys, meshesBuf, keysBuf);
|
||||
if (meshesBuf.length > n) meshesBuf.resize(n);
|
||||
}
|
||||
#else
|
||||
static inline function boolToInt(b: Bool): Int {
|
||||
return b ? 1 : 0;
|
||||
}
|
||||
@ -501,6 +574,7 @@ class RenderPath {
|
||||
return a.data.name > b.data.name ? 1 : -1;
|
||||
});
|
||||
}
|
||||
#end
|
||||
|
||||
public function drawMeshes(context: String) {
|
||||
var isShadows = context == "shadowmap" || context == "shadowmap_transparent";
|
||||
@ -519,6 +593,11 @@ class RenderPath {
|
||||
for (i in 0...LightObject.cascadeCount) {
|
||||
light.setCascade(Scene.active.camera, i);
|
||||
currentG.viewport(i * step, 0, step, step);
|
||||
#if lnx_occlusion_culling
|
||||
if (occlusionCulling && CullSystem.active != null) {
|
||||
CullSystem.active.cullShadow(light);
|
||||
}
|
||||
#end
|
||||
submitDraw(context);
|
||||
}
|
||||
drawn = true;
|
||||
@ -529,7 +608,14 @@ class RenderPath {
|
||||
if (context == "mesh") LightObject.updateClusters(Scene.active.camera);
|
||||
#end
|
||||
|
||||
if (!drawn) submitDraw(context);
|
||||
if (!drawn) {
|
||||
#if lnx_occlusion_culling
|
||||
if (isShadows && occlusionCulling && CullSystem.active != null) {
|
||||
CullSystem.active.cullShadow(light);
|
||||
}
|
||||
#end
|
||||
submitDraw(context);
|
||||
}
|
||||
|
||||
// Callbacks to specific context
|
||||
if (contextEvents != null) {
|
||||
@ -546,7 +632,34 @@ class RenderPath {
|
||||
var meshes = Scene.active.meshes;
|
||||
MeshObject.lastPipeline = null;
|
||||
|
||||
if (!meshesSorted && camera != null) { // Order max once per frame for now
|
||||
#if lnx_occlusion_culling
|
||||
if (!cullFlagsReset && !occlusionCulling) {
|
||||
cullFlagsReset = true;
|
||||
for (m in meshes) {
|
||||
m.occlusionVisible = true;
|
||||
m.shadowOcclusionVisible = true;
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
if (!cullComputed && (context == "mesh" || context == "depth")) {
|
||||
#if lnx_occlusion_culling
|
||||
if (occlusionCulling) {
|
||||
if (CullSystem.active == null) CullSystem.init();
|
||||
if (CullSystem.active != null && camera != null) {
|
||||
if (CullSystem.active.proxiesDirty) {
|
||||
CullSystem.active.loadOccluderProxies();
|
||||
CullSystem.active.proxiesDirty = false;
|
||||
}
|
||||
CullSystem.active.cull(camera);
|
||||
}
|
||||
}
|
||||
#end
|
||||
cullComputed = true;
|
||||
}
|
||||
|
||||
var isShadowCtx = context == "shadowmap" || context == "shadowmap_transparent";
|
||||
if (!meshesSorted && camera != null && !isShadowCtx) {
|
||||
var camX = camera.transform.worldx();
|
||||
var camY = camera.transform.worldy();
|
||||
var camZ = camera.transform.worldz();
|
||||
@ -571,8 +684,20 @@ class RenderPath {
|
||||
|
||||
static inline function meshRenderLoop(g: Graphics, context: String, _bindParams: Array<String>, _meshes: Array<MeshObject>) {
|
||||
var isReadingDepth = false;
|
||||
#if lnx_occlusion_culling
|
||||
var cullSys = CullSystem.active;
|
||||
#end
|
||||
|
||||
for (m in _meshes) {
|
||||
#if lnx_occlusion_culling
|
||||
if (cullSys != null && !m.occlusionVisible && (context == "mesh" || context == "depth" || context == "translucent")) {
|
||||
continue;
|
||||
}
|
||||
if (cullSys != null && !m.shadowOcclusionVisible && (context == "shadowmap" || context == "shadowmap_transparent")) {
|
||||
continue;
|
||||
}
|
||||
#end
|
||||
|
||||
#if rp_depth_texture
|
||||
// First mesh that reads depth
|
||||
if (!isReadingDepth && m.depthRead) {
|
||||
@ -692,7 +817,7 @@ class RenderPath {
|
||||
if (vr == null || vr._glContext == null || vr.xrGLLayer == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var gl: js.html.webgl.WebGL2RenderingContext = cast vr._glContext;
|
||||
var source = renderTargets.get(sourceTarget);
|
||||
if (source == null) {
|
||||
@ -703,21 +828,21 @@ class RenderPath {
|
||||
return;
|
||||
}
|
||||
// trace('Framebuffer OK');
|
||||
|
||||
|
||||
renderToXRFramebuffer = true;
|
||||
gl.bindFramebuffer(js.html.webgl.WebGL2RenderingContext.DRAW_FRAMEBUFFER, vr.xrGLLayer.framebuffer);
|
||||
gl.bindFramebuffer(js.html.webgl.WebGL2RenderingContext.READ_FRAMEBUFFER, sourceFB);
|
||||
|
||||
|
||||
var readStatus = gl.checkFramebufferStatus(js.html.webgl.WebGL2RenderingContext.READ_FRAMEBUFFER);
|
||||
var drawStatus = gl.checkFramebufferStatus(js.html.webgl.WebGL2RenderingContext.DRAW_FRAMEBUFFER);
|
||||
if (readStatus != js.html.webgl.WebGL2RenderingContext.FRAMEBUFFER_COMPLETE ||
|
||||
if (readStatus != js.html.webgl.WebGL2RenderingContext.FRAMEBUFFER_COMPLETE ||
|
||||
drawStatus != js.html.webgl.WebGL2RenderingContext.FRAMEBUFFER_COMPLETE) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var halfWidth = Std.int(source.image.width / 2);
|
||||
var fullHeight = source.image.height;
|
||||
|
||||
|
||||
if (vr._leftViewport != null) {
|
||||
var vp = vr._leftViewport;
|
||||
gl.blitFramebuffer(
|
||||
@ -736,7 +861,7 @@ class RenderPath {
|
||||
js.html.webgl.WebGL2RenderingContext.LINEAR
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
gl.bindFramebuffer(js.html.webgl.WebGL2RenderingContext.FRAMEBUFFER, null);
|
||||
renderToXRFramebuffer = false;
|
||||
#end
|
||||
@ -749,24 +874,24 @@ class RenderPath {
|
||||
if (currentG == null && frameG != null) {
|
||||
currentG = frameG;
|
||||
}
|
||||
|
||||
|
||||
var appw = iron.App.w();
|
||||
var apph = iron.App.h();
|
||||
var g = currentG;
|
||||
|
||||
|
||||
// get render target dimensions not App.w/h gbuffer is scaled in simulate mode with supersampling
|
||||
|
||||
var gbuffer0 = renderTargets.get("gbuffer0");
|
||||
var actualWidth = (gbuffer0 != null && gbuffer0.image != null) ? gbuffer0.image.width : appw;
|
||||
var actualHeight = (gbuffer0 != null && gbuffer0.image != null) ? gbuffer0.image.height : apph;
|
||||
var actualHalfWidth = Std.int(actualWidth / 2);
|
||||
|
||||
|
||||
var vrFBWidth = actualWidth;
|
||||
var vrFBHeight = actualHeight;
|
||||
var vrHalfWidth = actualHalfWidth;
|
||||
var isVRPresenting = false;
|
||||
vrSimulateMode = false;
|
||||
|
||||
|
||||
var vr:Dynamic = null;
|
||||
var vrExists = false;
|
||||
#if (kha_webgl && lnx_vr)
|
||||
@ -779,7 +904,7 @@ class RenderPath {
|
||||
if (vrExists && vr != null && vr.IsPresenting()) {
|
||||
vrSimulateMode = false;
|
||||
isVRPresenting = true;
|
||||
|
||||
|
||||
// get framebuffer dimensions from XR layer
|
||||
#if (kha_webgl && lnx_vr)
|
||||
var xrVr: kha.js.vr.VrInterface = cast vr;
|
||||
@ -789,16 +914,16 @@ class RenderPath {
|
||||
vrHalfWidth = Std.int(vrFBWidth / 2);
|
||||
}
|
||||
#end
|
||||
|
||||
|
||||
if (Scene.active == null || Scene.active.camera == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#if (kha_webgl && lnx_vr)
|
||||
if (vrCenterCameraWorld == null) vrCenterCameraWorld = Mat4.identity();
|
||||
vrCenterCameraWorld.setFrom(Scene.active.camera.transform.world);
|
||||
#end
|
||||
|
||||
|
||||
// LEFT EYE
|
||||
// HMD center for room scale position tracking
|
||||
#if (kha_webgl && lnx_vr)
|
||||
@ -808,10 +933,10 @@ class RenderPath {
|
||||
if (viewerTransform != null && viewerTransform.position != null) {
|
||||
// VR present calibration is used to position objects in world space not the camera
|
||||
var pos = viewerTransform.position;
|
||||
|
||||
|
||||
// camera follows headset directly in local floor space
|
||||
Scene.active.camera.transform.loc.set(pos.x, pos.y, pos.z);
|
||||
|
||||
|
||||
if (viewerTransform.orientation != null) {
|
||||
Scene.active.camera.transform.rot.set(
|
||||
viewerTransform.orientation.x,
|
||||
@ -825,7 +950,7 @@ class RenderPath {
|
||||
}
|
||||
iron.system.VRController.updatePoses();
|
||||
#end
|
||||
|
||||
|
||||
Scene.active.camera.V.self = vr.GetViewMatrix(0);
|
||||
Scene.active.camera.P.self = vr.GetProjectionMatrix(0);
|
||||
Scene.active.camera.VP.setFrom(Scene.active.camera.P);
|
||||
@ -835,7 +960,7 @@ class RenderPath {
|
||||
var renderWidth = actualWidth;
|
||||
var renderHeight = actualHeight;
|
||||
var renderHalfWidth = actualHalfWidth;
|
||||
|
||||
|
||||
// left half of render target
|
||||
g.viewport(0, 0, renderHalfWidth, renderHeight);
|
||||
g.scissor(0, 0, renderHalfWidth, renderHeight);
|
||||
@ -847,12 +972,12 @@ class RenderPath {
|
||||
Scene.active.camera.VP.setFrom(Scene.active.camera.P);
|
||||
Scene.active.camera.VP.multmat(Scene.active.camera.V);
|
||||
Scene.active.camera.buildMatrix();
|
||||
|
||||
|
||||
// right half of render target
|
||||
g.viewport(renderHalfWidth, 0, renderHalfWidth, renderHeight);
|
||||
g.scissor(renderHalfWidth, 0, renderHalfWidth, renderHeight);
|
||||
drawMeshes();
|
||||
|
||||
|
||||
// restore for post-processing
|
||||
g.disableScissor();
|
||||
g.viewport(0, 0, renderWidth, renderHeight);
|
||||
@ -865,7 +990,7 @@ class RenderPath {
|
||||
if (vrCenterCameraWorld == null) vrCenterCameraWorld = Mat4.identity();
|
||||
vrCenterCameraWorld.setFrom(Scene.active.camera.transform.world);
|
||||
#end
|
||||
|
||||
|
||||
Scene.active.camera.buildProjection(actualHalfWidth / actualHeight);
|
||||
|
||||
Scene.active.camera.transform.move(Scene.active.camera.right(), -ipd_offset);
|
||||
@ -908,7 +1033,9 @@ class RenderPath {
|
||||
#end
|
||||
|
||||
Data.getShader(shaderPath[0], shaderPath[1], function(res: ShaderData) {
|
||||
cc.context = res.getContext(shaderPath[2]);
|
||||
if (res != null) {
|
||||
cc.context = res.getContext(shaderPath[2]);
|
||||
}
|
||||
loading--;
|
||||
});
|
||||
}
|
||||
|
||||
@ -1003,7 +1003,7 @@ class Scene {
|
||||
}
|
||||
|
||||
if (o.vertex_groups != null) {
|
||||
cast(object, MeshObject).vertexGroups = new Map();
|
||||
object.vertexGroups = new Map();
|
||||
for (p in o.vertex_groups) {
|
||||
var verts:Array<iron.math.Vec4> = [];
|
||||
|
||||
@ -1014,7 +1014,7 @@ class Scene {
|
||||
verts.push(new iron.math.Vec4(data[i * 3], data[i * 3 + 1], data[i * 3 + 2], 1.0));
|
||||
}
|
||||
}
|
||||
cast(object, MeshObject).vertexGroups.set(p.name, verts);
|
||||
object.vertexGroups.set(p.name, verts);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
leenkx/Sources/iron/culling/BVHNode.hx
Normal file
20
leenkx/Sources/iron/culling/BVHNode.hx
Normal file
@ -0,0 +1,20 @@
|
||||
package iron.culling;
|
||||
|
||||
class BVHNode {
|
||||
public var minX: Float = 0;
|
||||
public var minY: Float = 0;
|
||||
public var minZ: Float = 0;
|
||||
public var maxX: Float = 0;
|
||||
public var maxY: Float = 0;
|
||||
public var maxZ: Float = 0;
|
||||
public var centerX: Float = 0;
|
||||
public var centerY: Float = 0;
|
||||
public var centerZ: Float = 0;
|
||||
public var isLeaf: Bool = false;
|
||||
public var leftChild: Int = -1;
|
||||
public var rightChild: Int = -1;
|
||||
public var firstObject: Int = 0;
|
||||
public var objectCount: Int = 0;
|
||||
|
||||
public function new() {}
|
||||
}
|
||||
62
leenkx/Sources/iron/culling/ContributionCull.hx
Normal file
62
leenkx/Sources/iron/culling/ContributionCull.hx
Normal file
@ -0,0 +1,62 @@
|
||||
package iron.culling;
|
||||
|
||||
class ContributionCull {
|
||||
|
||||
public static var minPixelRadius: Float = 2.0;
|
||||
public static var enabled: Bool = false;
|
||||
|
||||
public static function passes(
|
||||
centerX: Float, centerY: Float, centerZ: Float,
|
||||
radius: Float,
|
||||
camX: Float, camY: Float, camZ: Float,
|
||||
focalLengthPx: Float
|
||||
): Bool {
|
||||
if (!enabled || minPixelRadius <= 0.0 || focalLengthPx <= 0.0) return true;
|
||||
|
||||
var dx = centerX - camX;
|
||||
var dy = centerY - camY;
|
||||
var dz = centerZ - camZ;
|
||||
var dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
|
||||
if (dist <= radius) return true;
|
||||
|
||||
var projectedRadiusPx = focalLengthPx * radius / dist;
|
||||
return projectedRadiusPx >= minPixelRadius;
|
||||
}
|
||||
|
||||
public static function passesNode(
|
||||
node: BVHNode,
|
||||
camX: Float, camY: Float, camZ: Float,
|
||||
focalLengthPx: Float
|
||||
): Bool {
|
||||
if (!enabled || minPixelRadius <= 0.0 || focalLengthPx <= 0.0) return true;
|
||||
|
||||
var cx = (node.minX + node.maxX) * 0.5;
|
||||
var cy = (node.minY + node.maxY) * 0.5;
|
||||
var cz = (node.minZ + node.maxZ) * 0.5;
|
||||
var ex = (node.maxX - node.minX) * 0.5;
|
||||
var ey = (node.maxY - node.minY) * 0.5;
|
||||
var ez = (node.maxZ - node.minZ) * 0.5;
|
||||
var radius = Math.sqrt(ex * ex + ey * ey + ez * ez);
|
||||
|
||||
if (camX >= node.minX && camX <= node.maxX &&
|
||||
camY >= node.minY && camY <= node.maxY &&
|
||||
camZ >= node.minZ && camZ <= node.maxZ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
var dx = cx - camX;
|
||||
var dy = cy - camY;
|
||||
var dz = cz - camZ;
|
||||
var dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
|
||||
if (dist <= radius) return true;
|
||||
|
||||
var projectedRadiusPx = focalLengthPx * radius / dist;
|
||||
return projectedRadiusPx >= minPixelRadius;
|
||||
}
|
||||
|
||||
public static function computeFocalLengthPx(fov: Float, screenHeight: Int): Float {
|
||||
return screenHeight / (2.0 * Math.tan(fov * 0.5));
|
||||
}
|
||||
}
|
||||
373
leenkx/Sources/iron/culling/CullData.hx
Normal file
373
leenkx/Sources/iron/culling/CullData.hx
Normal file
@ -0,0 +1,373 @@
|
||||
package iron.culling;
|
||||
|
||||
import iron.object.MeshObject;
|
||||
import iron.Scene;
|
||||
import kha.arrays.Float32Array;
|
||||
|
||||
class CullData {
|
||||
|
||||
static inline var STRIDE = 16;
|
||||
static inline var BASIS_STRIDE = 12;
|
||||
|
||||
public var data: Float32Array;
|
||||
public var prevData: Float32Array;
|
||||
public var uidData: kha.arrays.Int32Array;
|
||||
public var prevUidData: kha.arrays.Int32Array;
|
||||
var basisData: Float32Array;
|
||||
var prevBasisData: Float32Array;
|
||||
public var count: Int = 0;
|
||||
public var prevCount: Int = 0;
|
||||
public var anyMoved: Bool = false;
|
||||
public var anyFlagsChanged: Bool = false;
|
||||
public var orderChanged: Bool = false;
|
||||
|
||||
public var meshes: Array<MeshObject> = [];
|
||||
public var slotsDirty: Bool = true;
|
||||
public var slotsRebuilt: Bool = false;
|
||||
|
||||
public var visibleList: Array<Int>;
|
||||
public var visibleCount: Int = 0;
|
||||
|
||||
var staticFlagsCache: haxe.ds.IntMap<Int> = new haxe.ds.IntMap<Int>();
|
||||
|
||||
var matTranspCache: haxe.ds.IntMap<Int> = new haxe.ds.IntMap<Int>();
|
||||
var matSnapshots: haxe.ds.IntMap<haxe.ds.Vector<iron.data.MaterialData>> = new haxe.ds.IntMap();
|
||||
|
||||
var prevUidMap: haxe.ds.IntMap<Int> = new haxe.ds.IntMap<Int>();
|
||||
|
||||
public function new() {
|
||||
visibleList = [];
|
||||
}
|
||||
|
||||
public inline function stride(): Int {
|
||||
return STRIDE;
|
||||
}
|
||||
|
||||
public function ensureCapacity(cap: Int) {
|
||||
if (data == null || data.length < cap * STRIDE) {
|
||||
var newCap = cap;
|
||||
if (data != null) newCap = Std.int(Math.max(cap, data.length / STRIDE * 2));
|
||||
var oldData = data;
|
||||
var oldPrev = prevData;
|
||||
var oldUid = uidData;
|
||||
var oldPrevUid = prevUidData;
|
||||
var oldBasis = basisData;
|
||||
var oldPrevBasis = prevBasisData;
|
||||
data = new Float32Array(newCap * STRIDE);
|
||||
prevData = new Float32Array(newCap * STRIDE);
|
||||
uidData = new kha.arrays.Int32Array(newCap);
|
||||
prevUidData = new kha.arrays.Int32Array(newCap);
|
||||
basisData = new Float32Array(newCap * BASIS_STRIDE);
|
||||
prevBasisData = new Float32Array(newCap * BASIS_STRIDE);
|
||||
if (oldData != null) {
|
||||
var copyLen = oldData.length;
|
||||
for (i in 0...copyLen) data[i] = oldData[i];
|
||||
}
|
||||
if (oldPrev != null) {
|
||||
var copyLen = oldPrev.length;
|
||||
for (i in 0...copyLen) prevData[i] = oldPrev[i];
|
||||
}
|
||||
if (oldUid != null) {
|
||||
var copyLen = oldUid.length;
|
||||
for (i in 0...copyLen) uidData[i] = oldUid[i];
|
||||
}
|
||||
if (oldPrevUid != null) {
|
||||
var copyLen = oldPrevUid.length;
|
||||
for (i in 0...copyLen) prevUidData[i] = oldPrevUid[i];
|
||||
}
|
||||
if (oldBasis != null) {
|
||||
var copyLen = oldBasis.length;
|
||||
for (i in 0...copyLen) basisData[i] = oldBasis[i];
|
||||
}
|
||||
if (oldPrevBasis != null) {
|
||||
var copyLen = oldPrevBasis.length;
|
||||
for (i in 0...copyLen) prevBasisData[i] = oldPrevBasis[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function syncFromScene() {
|
||||
var sceneMeshes = Scene.active.meshes;
|
||||
slotsRebuilt = false;
|
||||
if (slotsDirty || meshes.length != sceneMeshes.length) {
|
||||
meshes = sceneMeshes.copy();
|
||||
slotsDirty = false;
|
||||
slotsRebuilt = true;
|
||||
staticFlagsCache.clear();
|
||||
matTranspCache.clear();
|
||||
matSnapshots.clear();
|
||||
}
|
||||
count = meshes.length;
|
||||
ensureCapacity(count);
|
||||
|
||||
var tmpD = prevData; prevData = data; data = tmpD;
|
||||
var tmpU = prevUidData; prevUidData = uidData; uidData = tmpU;
|
||||
var tmpB = prevBasisData; prevBasisData = basisData; basisData = tmpB;
|
||||
|
||||
prevUidMap.clear();
|
||||
var prevUidMapBuilt = false;
|
||||
anyMoved = false;
|
||||
anyFlagsChanged = false;
|
||||
orderChanged = false;
|
||||
|
||||
for (i in 0...count) {
|
||||
var mesh = meshes[i];
|
||||
var t = mesh.transform;
|
||||
var uid = mesh.uid;
|
||||
var p = mesh.parent;
|
||||
while (p != null) {
|
||||
p.transform.update();
|
||||
p = p.parent;
|
||||
}
|
||||
t.update();
|
||||
|
||||
var off = i * STRIDE;
|
||||
var r = t.radius;
|
||||
var wx = t.worldx();
|
||||
var wy = t.worldy();
|
||||
var wz = t.worldz();
|
||||
|
||||
var radiusScale = mesh.data.isSkinned ? 2.0 : 1.0;
|
||||
#if lnx_gpu_particles
|
||||
if (mesh.particleSystems != null || mesh.particleOwner != null) radiusScale *= 1000;
|
||||
#end
|
||||
if (mesh.data.geom.instanced) radiusScale *= 100;
|
||||
r *= radiusScale;
|
||||
|
||||
data[off + 3] = r;
|
||||
data[off + 4] = wx;
|
||||
data[off + 5] = wy;
|
||||
data[off + 6] = wz;
|
||||
|
||||
var w = t.world;
|
||||
|
||||
var prevIdx: Null<Int> = null;
|
||||
if (prevUidData != null && i < prevCount && prevUidData[i] == uid) {
|
||||
prevIdx = i;
|
||||
} else {
|
||||
if (!prevUidMapBuilt) {
|
||||
prevUidMapBuilt = true;
|
||||
if (prevUidData != null) {
|
||||
for (pi in 0...prevCount) prevUidMap.set(prevUidData[pi], pi);
|
||||
}
|
||||
}
|
||||
prevIdx = prevUidMap.get(uid);
|
||||
}
|
||||
|
||||
if (prevIdx != null && prevIdx != i) orderChanged = true;
|
||||
var aabbCached = false;
|
||||
if (prevIdx != null) {
|
||||
var poff = prevIdx * STRIDE;
|
||||
var pb = prevIdx * BASIS_STRIDE;
|
||||
if (prevData[poff + 4] == wx && prevData[poff + 5] == wy && prevData[poff + 6] == wz &&
|
||||
prevBasisData[pb + 0] == w._00 && prevBasisData[pb + 1] == w._10 && prevBasisData[pb + 2] == w._20 &&
|
||||
prevBasisData[pb + 3] == w._01 && prevBasisData[pb + 4] == w._11 && prevBasisData[pb + 5] == w._21 &&
|
||||
prevBasisData[pb + 6] == w._02 && prevBasisData[pb + 7] == w._12 && prevBasisData[pb + 8] == w._22 &&
|
||||
prevBasisData[pb + 9] == t.dim.x && prevBasisData[pb + 10] == t.dim.y && prevBasisData[pb + 11] == t.dim.z) {
|
||||
data[off + 0] = prevData[poff + 0];
|
||||
data[off + 1] = prevData[poff + 1];
|
||||
data[off + 2] = prevData[poff + 2];
|
||||
data[off + 8] = prevData[poff + 8];
|
||||
data[off + 9] = prevData[poff + 9];
|
||||
data[off + 10] = prevData[poff + 10];
|
||||
aabbCached = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!aabbCached) {
|
||||
var r00 = w._00, r10 = w._10, r20 = w._20;
|
||||
var r01 = w._01, r11 = w._11, r21 = w._21;
|
||||
var r02 = w._02, r12 = w._12, r22 = w._22;
|
||||
var lenX = Math.sqrt(r00 * r00 + r01 * r01 + r02 * r02);
|
||||
var lenY = Math.sqrt(r10 * r10 + r11 * r11 + r12 * r12);
|
||||
var lenZ = Math.sqrt(r20 * r20 + r21 * r21 + r22 * r22);
|
||||
if (lenX > 1e-8) { r00 /= lenX; r01 /= lenX; r02 /= lenX; }
|
||||
if (lenY > 1e-8) { r10 /= lenY; r11 /= lenY; r12 /= lenY; }
|
||||
if (lenZ > 1e-8) { r20 /= lenZ; r21 /= lenZ; r22 /= lenZ; }
|
||||
|
||||
var hx = t.dim.x * 0.5 * radiusScale;
|
||||
var hy = t.dim.y * 0.5 * radiusScale;
|
||||
var hz = t.dim.z * 0.5 * radiusScale;
|
||||
|
||||
var aabbMinX = 1e20, aabbMinY = 1e20, aabbMinZ = 1e20;
|
||||
var aabbMaxX = -1e20, aabbMaxY = -1e20, aabbMaxZ = -1e20;
|
||||
for (c in 0...8) {
|
||||
var cx = (c & 1) != 0 ? hx : -hx;
|
||||
var cy = (c & 2) != 0 ? hy : -hy;
|
||||
var cz = (c & 4) != 0 ? hz : -hz;
|
||||
var px = cx * r00 + cy * r10 + cz * r20 + wx;
|
||||
var py = cx * r01 + cy * r11 + cz * r21 + wy;
|
||||
var pz = cx * r02 + cy * r12 + cz * r22 + wz;
|
||||
if (px < aabbMinX) aabbMinX = px;
|
||||
if (py < aabbMinY) aabbMinY = py;
|
||||
if (pz < aabbMinZ) aabbMinZ = pz;
|
||||
if (px > aabbMaxX) aabbMaxX = px;
|
||||
if (py > aabbMaxY) aabbMaxY = py;
|
||||
if (pz > aabbMaxZ) aabbMaxZ = pz;
|
||||
}
|
||||
|
||||
data[off + 0] = aabbMinX;
|
||||
data[off + 1] = aabbMinY;
|
||||
data[off + 2] = aabbMinZ;
|
||||
|
||||
data[off + 8] = aabbMaxX;
|
||||
data[off + 9] = aabbMaxY;
|
||||
data[off + 10] = aabbMaxZ;
|
||||
}
|
||||
|
||||
var flags: Int;
|
||||
var cachedFlags = staticFlagsCache.get(uid);
|
||||
if (cachedFlags != null) {
|
||||
flags = cachedFlags;
|
||||
} else {
|
||||
flags = 0;
|
||||
if (mesh.raw != null && mesh.raw.properties != null) {
|
||||
for (prop in mesh.raw.properties) {
|
||||
if (prop.name == "occluder" && prop.value == true) {
|
||||
flags |= 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((flags & 1) == 0 && mesh.data != null &&
|
||||
mesh.data.raw != null && mesh.data.raw.occluder_proxy != null) {
|
||||
flags |= 1;
|
||||
}
|
||||
if (mesh.raw != null && mesh.raw.visible_shadow != false) flags |= 4;
|
||||
staticFlagsCache.set(uid, flags);
|
||||
}
|
||||
var mats = mesh.materials;
|
||||
var snap = matSnapshots.get(uid);
|
||||
var matsChanged = snap == null || mats == null || snap.length != mats.length;
|
||||
if (!matsChanged) {
|
||||
for (mi in 0...mats.length) {
|
||||
if (snap[mi] != mats[mi]) { matsChanged = true; break; }
|
||||
}
|
||||
}
|
||||
if (matsChanged) {
|
||||
var tbit = 0;
|
||||
if (mats != null) {
|
||||
for (mi in 0...mats.length) {
|
||||
var mat = mats[mi];
|
||||
if (mat == null || mat.shader == null) continue;
|
||||
for (sc in mat.shader.contexts) {
|
||||
var sr = sc.raw;
|
||||
if (sr != null && sr.name != null && sr.name.substr(0, 4) == "mesh" &&
|
||||
(sr.depth_write != true || sr.blend_source != null)) {
|
||||
tbit = 32;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (tbit != 0) break;
|
||||
}
|
||||
}
|
||||
matTranspCache.set(uid, tbit);
|
||||
var nsnap = new haxe.ds.Vector<iron.data.MaterialData>(mats != null ? mats.length : 0);
|
||||
for (mi in 0...nsnap.length) nsnap[mi] = mats[mi];
|
||||
matSnapshots.set(uid, nsnap);
|
||||
}
|
||||
var tbitCached = matTranspCache.get(uid);
|
||||
if (tbitCached != null) flags |= tbitCached;
|
||||
if (mesh.visible) flags |= 8;
|
||||
if (radiusScale != 1.0) flags |= 16;
|
||||
if (prevIdx != null) {
|
||||
var poff = prevIdx * STRIDE;
|
||||
if (prevData[poff + 0] != data[off + 0] ||
|
||||
prevData[poff + 1] != data[off + 1] ||
|
||||
prevData[poff + 2] != data[off + 2] ||
|
||||
prevData[poff + 8] != data[off + 8] ||
|
||||
prevData[poff + 9] != data[off + 9] ||
|
||||
prevData[poff + 10] != data[off + 10]) {
|
||||
flags |= 2;
|
||||
anyMoved = true;
|
||||
}
|
||||
} else {
|
||||
flags |= 2;
|
||||
anyMoved = true;
|
||||
anyFlagsChanged = true;
|
||||
}
|
||||
if (prevIdx != null) {
|
||||
var poff = prevIdx * STRIDE;
|
||||
if (((flags ^ Std.int(prevData[poff + 7])) & ~2) != 0) anyFlagsChanged = true;
|
||||
}
|
||||
data[off + 7] = flags;
|
||||
|
||||
var boff = i * BASIS_STRIDE;
|
||||
basisData[boff + 0] = w._00;
|
||||
basisData[boff + 1] = w._10;
|
||||
basisData[boff + 2] = w._20;
|
||||
basisData[boff + 3] = w._01;
|
||||
basisData[boff + 4] = w._11;
|
||||
basisData[boff + 5] = w._21;
|
||||
basisData[boff + 6] = w._02;
|
||||
basisData[boff + 7] = w._12;
|
||||
basisData[boff + 8] = w._22;
|
||||
basisData[boff + 9] = t.dim.x;
|
||||
basisData[boff + 10] = t.dim.y;
|
||||
basisData[boff + 11] = t.dim.z;
|
||||
uidData[i] = uid;
|
||||
}
|
||||
|
||||
prevCount = count;
|
||||
}
|
||||
|
||||
public inline function getMinX(i: Int): Float {
|
||||
return data[i * STRIDE + 0];
|
||||
}
|
||||
public inline function getMinY(i: Int): Float {
|
||||
return data[i * STRIDE + 1];
|
||||
}
|
||||
public inline function getMinZ(i: Int): Float {
|
||||
return data[i * STRIDE + 2];
|
||||
}
|
||||
public inline function getRadius(i: Int): Float {
|
||||
return data[i * STRIDE + 3];
|
||||
}
|
||||
public inline function getCenterX(i: Int): Float {
|
||||
return data[i * STRIDE + 4];
|
||||
}
|
||||
public inline function getCenterY(i: Int): Float {
|
||||
return data[i * STRIDE + 5];
|
||||
}
|
||||
public inline function getCenterZ(i: Int): Float {
|
||||
return data[i * STRIDE + 6];
|
||||
}
|
||||
public inline function getFlags(i: Int): Int {
|
||||
return Std.int(data[i * STRIDE + 7]);
|
||||
}
|
||||
public inline function isOccluder(i: Int): Bool {
|
||||
return (getFlags(i) & 1) != 0;
|
||||
}
|
||||
public inline function isDynamic(i: Int): Bool {
|
||||
return (getFlags(i) & 2) != 0;
|
||||
}
|
||||
public inline function isShadowCaster(i: Int): Bool {
|
||||
return (getFlags(i) & 4) != 0;
|
||||
}
|
||||
public inline function isMeshVisible(i: Int): Bool {
|
||||
return (getFlags(i) & 8) != 0;
|
||||
}
|
||||
public inline function getObjectID(i: Int): Int {
|
||||
return uidData[i];
|
||||
}
|
||||
|
||||
public inline function getMaxX(i: Int): Float {
|
||||
return data[i * STRIDE + 8];
|
||||
}
|
||||
public inline function getMaxY(i: Int): Float {
|
||||
return data[i * STRIDE + 9];
|
||||
}
|
||||
public inline function getMaxZ(i: Int): Float {
|
||||
return data[i * STRIDE + 10];
|
||||
}
|
||||
|
||||
public function clearVisible() {
|
||||
visibleCount = 0;
|
||||
if (visibleList.length > 0) visibleList.resize(0);
|
||||
}
|
||||
|
||||
public function addVisible(objectIndex: Int) {
|
||||
visibleList.push(objectIndex);
|
||||
visibleCount++;
|
||||
}
|
||||
}
|
||||
773
leenkx/Sources/iron/culling/CullSystem.hx
Normal file
773
leenkx/Sources/iron/culling/CullSystem.hx
Normal file
@ -0,0 +1,773 @@
|
||||
package iron.culling;
|
||||
|
||||
import iron.object.CameraObject;
|
||||
import iron.object.FrustumPlane;
|
||||
import iron.Scene;
|
||||
import iron.RenderPath;
|
||||
import iron.math.Mat4;
|
||||
|
||||
class CullSystem {
|
||||
|
||||
public static var active: CullSystem = null;
|
||||
|
||||
public var tier: CullTier;
|
||||
public var cullData: CullData;
|
||||
public var rasterizer: SoftwareOcclusionRasterizer;
|
||||
public var shadowRasterizer: SoftwareOcclusionRasterizer;
|
||||
public var occluderProxies: Array<OccluderProxy>;
|
||||
public var bvh: ObjectBVH;
|
||||
public var temporal: TemporalState;
|
||||
|
||||
public var enabled: Bool = true;
|
||||
public var occlusionEnabled: Bool = true;
|
||||
public var bvhDirty: Bool = true;
|
||||
public var proxiesDirty: Bool = true;
|
||||
var dataSyncedFrame: Int = -1;
|
||||
|
||||
var focalLengthPx: Float = 500.0;
|
||||
var orthoPxPerUnit: Float = 0.0;
|
||||
var screenW: Int = 1920;
|
||||
var screenH: Int = 1080;
|
||||
|
||||
public static var maxOccluders: Int = 48;
|
||||
public static var maxOccluderTris: Int = 4000;
|
||||
public static var maxTrisPerProxy: Int = 768;
|
||||
public static var maxShadowOccluders: Int = 24;
|
||||
public static var maxShadowOccluderTris: Int = 1536;
|
||||
public static var boxOccluders: Bool = false;
|
||||
public static var shadowBoxOccluders: Bool = false;
|
||||
public static var shadowBoxShrink: Float = 0.7;
|
||||
public static var shadowCullingEnabled: Bool = true;
|
||||
public static var minOccluderPx: Float = 24.0;
|
||||
public static var boxShrink: Float = 0.5;
|
||||
public static var depthEps: Float = 1e-6;
|
||||
|
||||
public static var shadowCullInterval: Int = 2;
|
||||
|
||||
static inline var MAX_SHADOW_SLOTS = 8;
|
||||
var shadowPasses: haxe.ds.ObjectMap<iron.object.LightObject, Array<ShadowPassSlot>> = new haxe.ds.ObjectMap();
|
||||
var shadowCallIdx: Map<iron.object.LightObject, Int> = new Map();
|
||||
var shadowCallFrame: Map<iron.object.LightObject, Int> = new Map();
|
||||
var lastShadowSlot: ShadowPassSlot = null;
|
||||
var lastShadowFrame: Int = -1;
|
||||
var lastShadowCallIdx: Int = -1;
|
||||
#if lnx_csm
|
||||
var shadowVps: haxe.ds.ObjectMap<iron.object.LightObject, Mat4> = new haxe.ds.ObjectMap();
|
||||
#end
|
||||
var sceneRev: Int = 0;
|
||||
var sceneRevFrame: Int = -1;
|
||||
var frameCounter: Int = 0;
|
||||
var bvhSyncedFrame: Int = -1; // Frame when BVH was built/refit
|
||||
|
||||
var occlusionFrustumPlanes: Array<FrustumPlane> = null;
|
||||
var frustumPlanes: Array<FrustumPlane> = null;
|
||||
|
||||
var shadowFrustumPlanes: Array<FrustumPlane> = null;
|
||||
|
||||
var uidToProxy: haxe.ds.IntMap<OccluderProxy> = new haxe.ds.IntMap<OccluderProxy>();
|
||||
var uidScan: haxe.ds.IntMap<Bool> = new haxe.ds.IntMap<Bool>();
|
||||
|
||||
var prevVP: Mat4 = null;
|
||||
#if lnx_taa
|
||||
var unjitteredVP: Mat4 = null;
|
||||
#end
|
||||
public var shadowFlagsValid: Bool = false;
|
||||
|
||||
var inFrustumList: Array<Int> = [];
|
||||
var selIdx: Array<Int> = [];
|
||||
var selScore: Array<Float> = [];
|
||||
var selCount: Int = 0;
|
||||
|
||||
public function new() {
|
||||
cullData = new CullData();
|
||||
rasterizer = new SoftwareOcclusionRasterizer(128, 64);
|
||||
shadowRasterizer = new SoftwareOcclusionRasterizer(128, 128);
|
||||
occluderProxies = [];
|
||||
temporal = new TemporalState();
|
||||
tier = detectTier();
|
||||
}
|
||||
|
||||
static inline var MAT_EPS = 1e-7;
|
||||
|
||||
static inline function matEquals(a: Mat4, b: Mat4): Bool {
|
||||
return
|
||||
Math.abs(a._00 - b._00) < MAT_EPS && Math.abs(a._10 - b._10) < MAT_EPS &&
|
||||
Math.abs(a._20 - b._20) < MAT_EPS && Math.abs(a._30 - b._30) < MAT_EPS &&
|
||||
Math.abs(a._01 - b._01) < MAT_EPS && Math.abs(a._11 - b._11) < MAT_EPS &&
|
||||
Math.abs(a._21 - b._21) < MAT_EPS && Math.abs(a._31 - b._31) < MAT_EPS &&
|
||||
Math.abs(a._02 - b._02) < MAT_EPS && Math.abs(a._12 - b._12) < MAT_EPS &&
|
||||
Math.abs(a._22 - b._22) < MAT_EPS && Math.abs(a._32 - b._32) < MAT_EPS &&
|
||||
Math.abs(a._03 - b._03) < MAT_EPS && Math.abs(a._13 - b._13) < MAT_EPS &&
|
||||
Math.abs(a._23 - b._23) < MAT_EPS && Math.abs(a._33 - b._33) < MAT_EPS;
|
||||
}
|
||||
|
||||
public static function detectTier(): CullTier {
|
||||
#if (target.threaded)
|
||||
return CPUMultiThread;
|
||||
#else
|
||||
return CPUSingleThread;
|
||||
#end
|
||||
}
|
||||
|
||||
public function cull(camera: CameraObject) {
|
||||
frameCounter = RenderPath.active != null ? RenderPath.active.frame : frameCounter + 1;
|
||||
if (!enabled || Scene.active == null || Scene.active.meshes.length == 0) {
|
||||
if (Scene.active != null) {
|
||||
var meshes = Scene.active.meshes;
|
||||
for (i in 0...meshes.length) {
|
||||
meshes[i].occlusionVisible = true;
|
||||
meshes[i].shadowOcclusionVisible = true;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var rp = RenderPath.active;
|
||||
if (rp != null) {
|
||||
screenW = rp.currentW;
|
||||
screenH = rp.currentH;
|
||||
}
|
||||
|
||||
var targetW = Std.int(Math.max(128, Math.min(512, screenW >> 4)));
|
||||
var targetH = Std.int(Math.max(64, Math.min(256, screenH >> 4)));
|
||||
if (rasterizer.width < (targetW >> 1) || rasterizer.width > (targetW << 1) ||
|
||||
rasterizer.height < (targetH >> 1) || rasterizer.height > (targetH << 1)) {
|
||||
rasterizer = new SoftwareOcclusionRasterizer(targetW, targetH);
|
||||
}
|
||||
|
||||
if (camera.data.raw.ortho == null) {
|
||||
focalLengthPx = ContributionCull.computeFocalLengthPx(
|
||||
camera.data.raw.fov, screenH
|
||||
);
|
||||
orthoPxPerUnit = 0.0;
|
||||
} else {
|
||||
focalLengthPx = 0;
|
||||
var o = camera.data.raw.ortho;
|
||||
var halfH = (o[3] - o[2]) * 0.5;
|
||||
orthoPxPerUnit = halfH > 1e-9 ? screenH * 0.5 / halfH : 0.0;
|
||||
}
|
||||
rasterizer.nearClip = camera.data.raw.ortho == null ?
|
||||
camera.data.raw.near_plane : 0.0001;
|
||||
|
||||
if (dataSyncedFrame != frameCounter) {
|
||||
cullData.syncFromScene();
|
||||
dataSyncedFrame = frameCounter;
|
||||
}
|
||||
if (cullData.slotsRebuilt) temporal.clear();
|
||||
if (sceneRevFrame != frameCounter) {
|
||||
sceneRevFrame = frameCounter;
|
||||
if (cullData.anyMoved || cullData.anyFlagsChanged || cullData.orderChanged) sceneRev++;
|
||||
}
|
||||
var cd = cullData;
|
||||
var meshes = cd.meshes;
|
||||
|
||||
#if lnx_taa
|
||||
if (unjitteredVP == null) unjitteredVP = Mat4.identity();
|
||||
unjitteredVP.multmats(camera.noJitterP, camera.V);
|
||||
var cmpVP = unjitteredVP;
|
||||
#else
|
||||
var cmpVP = camera.VP;
|
||||
#end
|
||||
var vpSame = prevVP != null && matEquals(cmpVP, prevVP);
|
||||
if (vpSame && !cd.anyMoved && !cd.anyFlagsChanged && !cd.orderChanged && !bvhDirty) {
|
||||
return;
|
||||
}
|
||||
var needBvhRebuild = bvhDirty;
|
||||
bvhDirty = false;
|
||||
if (prevVP == null) prevVP = Mat4.identity();
|
||||
prevVP.setFrom(cmpVP);
|
||||
|
||||
if (occlusionFrustumPlanes == null) {
|
||||
occlusionFrustumPlanes = [];
|
||||
for (i in 0...6) occlusionFrustumPlanes.push(new FrustumPlane());
|
||||
}
|
||||
{
|
||||
var vp = camera.VP;
|
||||
var p = occlusionFrustumPlanes;
|
||||
p[0].setComponents(vp._03 + vp._00, vp._13 + vp._10, vp._23 + vp._20, vp._33 + vp._30);
|
||||
p[1].setComponents(vp._03 - vp._00, vp._13 - vp._10, vp._23 - vp._20, vp._33 - vp._30);
|
||||
p[2].setComponents(vp._03 - vp._01, vp._13 - vp._11, vp._23 - vp._21, vp._33 - vp._31);
|
||||
p[3].setComponents(vp._03 + vp._01, vp._13 + vp._11, vp._23 + vp._21, vp._33 + vp._31);
|
||||
p[4].setComponents(vp._02, vp._12, vp._22, vp._32);
|
||||
p[5].setComponents(vp._03 - vp._02, vp._13 - vp._12, vp._23 - vp._22, vp._33 - vp._32);
|
||||
}
|
||||
frustumPlanes = occlusionFrustumPlanes;
|
||||
|
||||
var camX = camera.transform.worldx();
|
||||
var camY = camera.transform.worldy();
|
||||
var camZ = camera.transform.worldz();
|
||||
|
||||
if (bvh == null) bvh = new ObjectBVH();
|
||||
if (bvhSyncedFrame != frameCounter) {
|
||||
if (needBvhRebuild || bvh.root < 0) {
|
||||
bvh.build(cd);
|
||||
} else if (cd.anyMoved || cd.orderChanged) {
|
||||
bvh.refit(cd);
|
||||
}
|
||||
bvhSyncedFrame = frameCounter;
|
||||
}
|
||||
inFrustumList.resize(0);
|
||||
var planes = frustumPlanes;
|
||||
var fpx = focalLengthPx;
|
||||
bvh.traverseFrontToBack(camX, camY, camZ,
|
||||
function(node: BVHNode): Bool {
|
||||
if (!ObjectBVH.aabbInFrustum(node, planes)) return true;
|
||||
if (!ContributionCull.passesNode(node, camX, camY, camZ, fpx)) return true;
|
||||
return false;
|
||||
},
|
||||
function(objIdx: Int, _: Int) {
|
||||
if ((cd.getFlags(objIdx) & 8) == 0) return;
|
||||
if (!ContributionCull.passes(
|
||||
cd.getCenterX(objIdx), cd.getCenterY(objIdx), cd.getCenterZ(objIdx),
|
||||
cd.getRadius(objIdx), camX, camY, camZ, fpx
|
||||
)) return;
|
||||
inFrustumList.push(objIdx);
|
||||
}
|
||||
);
|
||||
cullData.clearVisible();
|
||||
|
||||
if (occlusionEnabled) {
|
||||
selectOccluders(meshes, camX, camY, camZ);
|
||||
|
||||
for (i in 0...meshes.length) {
|
||||
meshes[i].occlusionVisible = true;
|
||||
}
|
||||
|
||||
rasterizer.clear();
|
||||
if (selCount > 0) rasterizeSelected(meshes, camera.VP);
|
||||
var trisDone = rasterizer.trianglesRasterized;
|
||||
if (trisDone > 0) rasterizer.buildHiZ();
|
||||
|
||||
if (trisDone > 0) {
|
||||
testVisibilityBVH(camera.VP, camX, camY, camZ, planes);
|
||||
} else {
|
||||
for (k in 0...inFrustumList.length) {
|
||||
var objIdx = inFrustumList[k];
|
||||
cd.addVisible(objIdx);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (i in 0...meshes.length) {
|
||||
meshes[i].occlusionVisible = true;
|
||||
}
|
||||
for (k in 0...inFrustumList.length) {
|
||||
var objIdx = inFrustumList[k];
|
||||
cd.addVisible(objIdx);
|
||||
}
|
||||
}
|
||||
|
||||
temporal.advanceFrame();
|
||||
}
|
||||
|
||||
public function cullShadow(light: iron.object.LightObject) {
|
||||
if (!occlusionEnabled || light == null || Scene.active == null) return;
|
||||
if (!shadowCullingEnabled) {
|
||||
var ms = Scene.active.meshes;
|
||||
for (m in ms) m.shadowOcclusionVisible = true;
|
||||
return;
|
||||
}
|
||||
|
||||
var frame = RenderPath.active != null ? RenderPath.active.frame : frameCounter;
|
||||
var vp = light.VP;
|
||||
if (vp == null) return;
|
||||
|
||||
var cd = cullData;
|
||||
|
||||
if (dataSyncedFrame != frame) {
|
||||
cd.syncFromScene();
|
||||
dataSyncedFrame = frame;
|
||||
}
|
||||
if (sceneRevFrame != frame) {
|
||||
sceneRevFrame = frame;
|
||||
if (cd.anyMoved || cd.anyFlagsChanged || cd.orderChanged) sceneRev++;
|
||||
}
|
||||
var meshes = cd.meshes;
|
||||
|
||||
var slots = shadowPasses.get(light);
|
||||
if (slots == null) {
|
||||
slots = [];
|
||||
shadowPasses.set(light, slots);
|
||||
}
|
||||
var callIdx = 0;
|
||||
{
|
||||
var f = shadowCallFrame.get(light);
|
||||
callIdx = (f == frame) ? shadowCallIdx.get(light) + 1 : 0;
|
||||
shadowCallIdx.set(light, callIdx);
|
||||
shadowCallFrame.set(light, frame);
|
||||
}
|
||||
var slotIdx = callIdx < MAX_SHADOW_SLOTS ? callIdx : MAX_SHADOW_SLOTS - 1;
|
||||
while (slots.length <= slotIdx) slots.push(new ShadowPassSlot());
|
||||
var slot = slots[slotIdx];
|
||||
|
||||
if (slot != null && slot == lastShadowSlot && lastShadowFrame == frame &&
|
||||
lastShadowCallIdx == callIdx && slot.appliedRev == sceneRev) return;
|
||||
|
||||
for (i in 0...meshes.length) {
|
||||
meshes[i].shadowOcclusionVisible = true;
|
||||
}
|
||||
|
||||
#if lnx_csm
|
||||
var isCsmLight = light.data.raw.type == "sun" &&
|
||||
iron.object.LightObject.cascadeCount > 1;
|
||||
if (isCsmLight && callIdx == 0) {
|
||||
var vp0 = shadowVps.get(light);
|
||||
if (vp0 == null) { vp0 = Mat4.identity(); shadowVps.set(light, vp0); }
|
||||
vp0.setFrom(vp);
|
||||
}
|
||||
#end
|
||||
|
||||
if (shadowCullInterval > 1) {
|
||||
if ((frame + slotIdx) % shadowCullInterval != 0) {
|
||||
var applied = false;
|
||||
#if lnx_csm
|
||||
if (isCsmLight && callIdx > 0) {
|
||||
var head = slots[0];
|
||||
var vp0 = shadowVps.get(light);
|
||||
if (head.appliedRev == sceneRev && head.flagsLen > 0 &&
|
||||
head.vp != null && vp0 != null && matEquals(vp0, head.vp)) {
|
||||
var hn = head.flagsLen < meshes.length ? head.flagsLen : meshes.length;
|
||||
for (i in 0...hn) meshes[i].shadowOcclusionVisible = head.flags[i];
|
||||
}
|
||||
applied = true;
|
||||
}
|
||||
#end
|
||||
if (!applied && slot.appliedRev == sceneRev && slot.flagsLen > 0 &&
|
||||
slot.vp != null && matEquals(vp, slot.vp)) {
|
||||
var n = slot.flagsLen < meshes.length ? slot.flagsLen : meshes.length;
|
||||
for (i in 0...n) meshes[i].shadowOcclusionVisible = slot.flags[i];
|
||||
}
|
||||
lastShadowSlot = slot;
|
||||
lastShadowFrame = frame;
|
||||
lastShadowCallIdx = callIdx;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (bvh == null) bvh = new ObjectBVH();
|
||||
if (bvhSyncedFrame != frame) {
|
||||
if (bvhDirty || bvh.root < 0) {
|
||||
bvh.build(cd);
|
||||
} else if (cd.anyMoved || cd.orderChanged) {
|
||||
bvh.refit(cd);
|
||||
}
|
||||
bvhSyncedFrame = frame;
|
||||
}
|
||||
|
||||
if (shadowFrustumPlanes == null) {
|
||||
shadowFrustumPlanes = [];
|
||||
for (i in 0...6) shadowFrustumPlanes.push(new FrustumPlane());
|
||||
}
|
||||
{
|
||||
var p = shadowFrustumPlanes;
|
||||
p[0].setComponents(vp._03 + vp._00, vp._13 + vp._10, vp._23 + vp._20, vp._33 + vp._30);
|
||||
p[1].setComponents(vp._03 - vp._00, vp._13 - vp._10, vp._23 - vp._20, vp._33 - vp._30);
|
||||
p[2].setComponents(vp._03 - vp._01, vp._13 - vp._11, vp._23 - vp._21, vp._33 - vp._31);
|
||||
p[3].setComponents(vp._03 + vp._01, vp._13 + vp._11, vp._23 + vp._21, vp._33 + vp._31);
|
||||
p[4].setComponents(vp._02, vp._12, vp._22, vp._32);
|
||||
p[5].setComponents(vp._03 - vp._02, vp._13 - vp._12, vp._23 - vp._22, vp._33 - vp._32);
|
||||
}
|
||||
var lightPlanes = shadowFrustumPlanes;
|
||||
|
||||
var lx = light.transform.worldx();
|
||||
var ly = light.transform.worldy();
|
||||
var lz = light.transform.worldz();
|
||||
|
||||
var raster = slot.rasterizer;
|
||||
var vpSame = slot.vp != null && matEquals(vp, slot.vp);
|
||||
if (slot.sceneRev == sceneRev && vpSame && raster != null) {
|
||||
// skip
|
||||
} else {
|
||||
if (raster == null) {
|
||||
raster = new SoftwareOcclusionRasterizer(
|
||||
shadowRasterizer.width, shadowRasterizer.height);
|
||||
slot.rasterizer = raster;
|
||||
}
|
||||
if (slot.vp == null) slot.vp = Mat4.identity();
|
||||
slot.vp.setFrom(vp);
|
||||
|
||||
var perspective = light.data.raw.type != "sun";
|
||||
selCount = 0;
|
||||
bvh.traverseFrontToBack(lx, ly, lz,
|
||||
function(node: BVHNode): Bool {
|
||||
if (!ObjectBVH.aabbInFrustum(node, lightPlanes)) return true;
|
||||
return false;
|
||||
},
|
||||
function(i: Int, _: Int) {
|
||||
if ((cd.getFlags(i) & 8) == 0) return;
|
||||
if ((cd.getFlags(i) & 16) != 0) return;
|
||||
if ((cd.getFlags(i) & 4) == 0) return;
|
||||
if ((cd.getFlags(i) & 32) != 0) return;
|
||||
if (i >= meshes.length) return;
|
||||
var r = cd.getRadius(i);
|
||||
var score = r;
|
||||
if (perspective) {
|
||||
var dx = cd.getCenterX(i) - lx;
|
||||
var dy = cd.getCenterY(i) - ly;
|
||||
var dz = cd.getCenterZ(i) - lz;
|
||||
var dist2 = dx * dx + dy * dy + dz * dz;
|
||||
if (dist2 <= r * r) return;
|
||||
score = r / Math.sqrt(dist2);
|
||||
}
|
||||
insertTopK(i, score, maxShadowOccluders);
|
||||
}
|
||||
);
|
||||
|
||||
raster = slot.rasterizer;
|
||||
raster.clear();
|
||||
var triBudget = maxShadowOccluderTris;
|
||||
for (s in 0...selCount) {
|
||||
if (triBudget < 12) break;
|
||||
var i = selIdx[s];
|
||||
var proxy = uidToProxy.get(cd.getObjectID(i));
|
||||
if (proxy != null && proxy.triangleCount > 0 &&
|
||||
proxy.triangleCount <= maxTrisPerProxy && proxy.triangleCount <= triBudget) {
|
||||
proxy.rasterize(raster, meshes[i].transform.world, vp,
|
||||
raster.width, raster.height);
|
||||
triBudget -= proxy.triangleCount;
|
||||
} else if (shadowBoxOccluders) {
|
||||
rasterizeBoxOccluder(raster, i, vp, shadowBoxShrink);
|
||||
triBudget -= 12;
|
||||
}
|
||||
}
|
||||
if (raster.trianglesRasterized > 0) raster.buildHiZ();
|
||||
}
|
||||
|
||||
if (raster.trianglesRasterized > 0) {
|
||||
var sw = raster.width;
|
||||
var sh = raster.height;
|
||||
|
||||
bvh.traverseFrontToBack(lx, ly, lz,
|
||||
function(node: BVHNode): Bool {
|
||||
if (!ObjectBVH.aabbInFrustum(node, lightPlanes)) return true;
|
||||
var anyVisible = false;
|
||||
if (node.isLeaf) {
|
||||
for (j in 0...node.objectCount) {
|
||||
var idx = bvh.objectIndices[node.firstObject + j];
|
||||
if ((cd.getFlags(idx) & 8) != 0 && idx < meshes.length) {
|
||||
anyVisible = true; break;
|
||||
}
|
||||
}
|
||||
if (!anyVisible) return true;
|
||||
}
|
||||
if (raster.isOccluded(
|
||||
node.minX, node.minY, node.minZ,
|
||||
node.maxX, node.maxY, node.maxZ,
|
||||
vp, sw, sh
|
||||
)) {
|
||||
bvh.forEachLeafInSubtree(node, function(idx: Int) {
|
||||
if (idx < meshes.length && (cd.getFlags(idx) & 8) != 0) {
|
||||
meshes[idx].shadowOcclusionVisible = false;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
function(objIdx: Int, _: Int) {
|
||||
if ((cd.getFlags(objIdx) & 8) == 0) return;
|
||||
if (objIdx >= meshes.length) return;
|
||||
meshes[objIdx].shadowOcclusionVisible = true;
|
||||
if (raster.isOccluded(
|
||||
cd.getMinX(objIdx), cd.getMinY(objIdx), cd.getMinZ(objIdx),
|
||||
cd.getMaxX(objIdx), cd.getMaxY(objIdx), cd.getMaxZ(objIdx),
|
||||
vp, sw, sh
|
||||
)) {
|
||||
meshes[objIdx].shadowOcclusionVisible = false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#if lnx_csm
|
||||
if (isCsmLight) {
|
||||
for (c in 1...iron.object.LightObject.cascadeCount) {
|
||||
if (c >= slots.length) break;
|
||||
var s = slots[c];
|
||||
if (s == null || s == slot || s.vp == null || s.rasterizer == null) continue;
|
||||
for (i in 0...meshes.length) {
|
||||
if (meshes[i].shadowOcclusionVisible) continue;
|
||||
if (!s.rasterizer.isOccluded(
|
||||
cd.getMinX(i), cd.getMinY(i), cd.getMinZ(i),
|
||||
cd.getMaxX(i), cd.getMaxY(i), cd.getMaxZ(i),
|
||||
s.vp, s.rasterizer.width, s.rasterizer.height
|
||||
)) {
|
||||
meshes[i].shadowOcclusionVisible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
if (slot.flags.length < meshes.length) slot.flags.resize(meshes.length);
|
||||
slot.flagsLen = meshes.length;
|
||||
for (i in 0...meshes.length) slot.flags[i] = meshes[i].shadowOcclusionVisible;
|
||||
|
||||
#if lnx_csm
|
||||
if (isCsmLight && callIdx > 0) {
|
||||
var head = slots[0];
|
||||
var vp0 = shadowVps.get(light);
|
||||
if (head.appliedRev == sceneRev && head.flagsLen > 0 &&
|
||||
head.vp != null && vp0 != null && matEquals(vp0, head.vp)) {
|
||||
var hn = head.flagsLen < meshes.length ? head.flagsLen : meshes.length;
|
||||
for (i in 0...hn) meshes[i].shadowOcclusionVisible = head.flags[i];
|
||||
} else {
|
||||
for (i in 0...meshes.length) meshes[i].shadowOcclusionVisible = true;
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
slot.sceneRev = sceneRev;
|
||||
slot.appliedRev = sceneRev;
|
||||
slot.lastUsed = frame;
|
||||
lastShadowSlot = slot;
|
||||
lastShadowFrame = frame;
|
||||
lastShadowCallIdx = callIdx;
|
||||
shadowFlagsValid = true;
|
||||
}
|
||||
|
||||
function selectOccluders(
|
||||
meshes: Array<iron.object.MeshObject>,
|
||||
camX: Float, camY: Float, camZ: Float
|
||||
) {
|
||||
var cd = cullData;
|
||||
selCount = 0;
|
||||
var minPx = minOccluderPx;
|
||||
var autoPx = autoOccluderMinScreenRatio * screenH;
|
||||
for (k in 0...inFrustumList.length) {
|
||||
var i = inFrustumList[k];
|
||||
if (i >= meshes.length) continue;
|
||||
if (!meshes[i].occlusionVisible) continue;
|
||||
var flags = cd.getFlags(i);
|
||||
if ((flags & 16) != 0) continue;
|
||||
if ((flags & 32) != 0) continue;
|
||||
var r = cd.getRadius(i);
|
||||
var px: Float;
|
||||
var camInside = false;
|
||||
if (focalLengthPx > 0) {
|
||||
var dx = cd.getCenterX(i) - camX;
|
||||
var dy = cd.getCenterY(i) - camY;
|
||||
var dz = cd.getCenterZ(i) - camZ;
|
||||
var dist2 = dx * dx + dy * dy + dz * dz;
|
||||
if (dist2 <= r * r) camInside = true;
|
||||
px = camInside ? 1e6 : focalLengthPx * r / Math.sqrt(dist2);
|
||||
} else {
|
||||
px = r * orthoPxPerUnit;
|
||||
}
|
||||
if (px < minPx) continue;
|
||||
if ((flags & 1) == 0 && px < autoPx) continue;
|
||||
if (camInside || !boxOccluders) {
|
||||
var p = uidToProxy.get(cd.getObjectID(i));
|
||||
if (p == null || p.triangleCount <= 0 || p.triangleCount > maxTrisPerProxy) continue;
|
||||
}
|
||||
insertTopK(i, px, maxOccluders);
|
||||
}
|
||||
}
|
||||
|
||||
function insertTopK(idx: Int, score: Float, k: Int) {
|
||||
if (selIdx.length < k + 1) {
|
||||
selIdx.resize(k + 1);
|
||||
selScore.resize(k + 1);
|
||||
}
|
||||
var pos = selCount;
|
||||
while (pos > 0 && selScore[pos - 1] < score) pos--;
|
||||
if (pos >= k) return;
|
||||
var last = selCount < k ? selCount : k - 1;
|
||||
var j = last;
|
||||
while (j > pos) {
|
||||
selIdx[j] = selIdx[j - 1];
|
||||
selScore[j] = selScore[j - 1];
|
||||
j--;
|
||||
}
|
||||
selIdx[pos] = idx;
|
||||
selScore[pos] = score;
|
||||
if (selCount < k) selCount++;
|
||||
}
|
||||
|
||||
function rasterizeSelected(meshes: Array<iron.object.MeshObject>, vp: Mat4) {
|
||||
var cd = cullData;
|
||||
var triBudget = maxOccluderTris;
|
||||
#if (target.threaded)
|
||||
rasterizer.beginCollect();
|
||||
#end
|
||||
for (s in 0...selCount) {
|
||||
if (triBudget < 12) break;
|
||||
var i = selIdx[s];
|
||||
var proxy = uidToProxy.get(cd.getObjectID(i));
|
||||
if (proxy != null && proxy.triangleCount > 0 &&
|
||||
proxy.triangleCount <= maxTrisPerProxy && proxy.triangleCount <= triBudget) {
|
||||
var mesh = i < meshes.length ? meshes[i] : null;
|
||||
if (mesh == null) continue;
|
||||
proxy.rasterize(rasterizer, mesh.transform.world, vp, screenW, screenH);
|
||||
triBudget -= proxy.triangleCount;
|
||||
} else {
|
||||
if (!boxOccluders) continue;
|
||||
rasterizeBoxOccluder(rasterizer, i, vp);
|
||||
triBudget -= 12;
|
||||
}
|
||||
}
|
||||
#if (target.threaded)
|
||||
rasterizer.flushCollected();
|
||||
#end
|
||||
}
|
||||
|
||||
function rasterizeBoxOccluder(raster: SoftwareOcclusionRasterizer, objectIndex: Int, vp: Mat4, shrinkOverride = -1.0) {
|
||||
var cd = cullData;
|
||||
var bx0 = cd.getMinX(objectIndex), by0 = cd.getMinY(objectIndex), bz0 = cd.getMinZ(objectIndex);
|
||||
var bx1 = cd.getMaxX(objectIndex), by1 = cd.getMaxY(objectIndex), bz1 = cd.getMaxZ(objectIndex);
|
||||
var cx = (bx0 + bx1) * 0.5;
|
||||
var cy = (by0 + by1) * 0.5;
|
||||
var cz = (bz0 + bz1) * 0.5;
|
||||
var boxShrink = shrinkOverride >= 0.0 ? shrinkOverride : CullSystem.boxShrink;
|
||||
var hx = (bx1 - bx0) * 0.5 * boxShrink;
|
||||
var hy = (by1 - by0) * 0.5 * boxShrink;
|
||||
var hz = (bz1 - bz0) * 0.5 * boxShrink;
|
||||
raster.rasterizeBox(
|
||||
cx - hx, cy - hy, cz - hz,
|
||||
cx + hx, cy + hy, cz + hz,
|
||||
vp, raster.width, raster.height
|
||||
);
|
||||
}
|
||||
|
||||
function testVisibilityBVH(
|
||||
vp: Mat4, camX: Float, camY: Float, camZ: Float,
|
||||
planes: Array<FrustumPlane>
|
||||
) {
|
||||
var cd = cullData;
|
||||
var raster = rasterizer;
|
||||
var sw = screenW;
|
||||
var sh = screenH;
|
||||
var meshes = cd.meshes;
|
||||
var bvh = this.bvh;
|
||||
var temp = temporal;
|
||||
var retestInterval = 4;
|
||||
|
||||
bvh.traverseFrontToBack(camX, camY, camZ,
|
||||
function(node: BVHNode): Bool {
|
||||
if (!ObjectBVH.aabbInFrustum(node, planes)) return true;
|
||||
var anyVisible = false;
|
||||
if (node.isLeaf) {
|
||||
for (j in 0...node.objectCount) {
|
||||
var idx = bvh.objectIndices[node.firstObject + j];
|
||||
if ((cd.getFlags(idx) & 8) != 0) { anyVisible = true; break; }
|
||||
}
|
||||
if (!anyVisible) return true;
|
||||
}
|
||||
if (raster.isOccluded(
|
||||
node.minX, node.minY, node.minZ,
|
||||
node.maxX, node.maxY, node.maxZ,
|
||||
vp, sw, sh
|
||||
)) {
|
||||
bvh.forEachLeafInSubtree(node, function(idx: Int) {
|
||||
if (idx >= meshes.length || (cd.getFlags(idx) & 8) == 0) return;
|
||||
meshes[idx].occlusionVisible = false;
|
||||
var uid = cd.getObjectID(idx);
|
||||
temp.setVisible(uid, false);
|
||||
temp.markTested(uid);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
function(objIdx: Int, _: Int) {
|
||||
if ((cd.getFlags(objIdx) & 8) == 0) return;
|
||||
if (objIdx >= meshes.length) return;
|
||||
var uid = cd.getObjectID(objIdx);
|
||||
if (temp.wasVisibleLastFrame(uid) && !temp.needsVisibleReTest(uid, retestInterval)) {
|
||||
meshes[objIdx].occlusionVisible = true;
|
||||
cd.addVisible(objIdx);
|
||||
return;
|
||||
}
|
||||
if (raster.isOccluded(
|
||||
cd.getMinX(objIdx), cd.getMinY(objIdx), cd.getMinZ(objIdx),
|
||||
cd.getMaxX(objIdx), cd.getMaxY(objIdx), cd.getMaxZ(objIdx),
|
||||
vp, sw, sh
|
||||
)) {
|
||||
meshes[objIdx].occlusionVisible = false;
|
||||
temp.setVisible(uid, false);
|
||||
temp.markTested(uid);
|
||||
} else {
|
||||
meshes[objIdx].occlusionVisible = true;
|
||||
cd.addVisible(objIdx);
|
||||
temp.setVisible(uid, true);
|
||||
temp.markTested(uid);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static var autoOccluderMinScreenRatio: Float = 0.10;
|
||||
|
||||
public inline function isVisible(objectIndex: Int): Bool {
|
||||
var ms = cullData.meshes;
|
||||
return objectIndex >= 0 && objectIndex < ms.length && ms[objectIndex].occlusionVisible;
|
||||
}
|
||||
|
||||
public inline function markDirty() {
|
||||
bvhDirty = true;
|
||||
proxiesDirty = true;
|
||||
cullData.slotsDirty = true;
|
||||
shadowFlagsValid = false;
|
||||
}
|
||||
|
||||
public function removeLight(light: iron.object.LightObject) {
|
||||
shadowPasses.remove(light);
|
||||
shadowCallIdx.remove(light);
|
||||
shadowCallFrame.remove(light);
|
||||
#if lnx_csm
|
||||
shadowVps.remove(light);
|
||||
#end
|
||||
}
|
||||
|
||||
public static function init() {
|
||||
if (active == null) {
|
||||
active = new CullSystem();
|
||||
}
|
||||
}
|
||||
|
||||
public function loadOccluderProxies() {
|
||||
occluderProxies.resize(0);
|
||||
if (Scene.active == null) {
|
||||
uidToProxy.clear();
|
||||
return;
|
||||
}
|
||||
var meshes = Scene.active.meshes;
|
||||
uidScan.clear();
|
||||
for (i in 0...meshes.length) {
|
||||
var mesh = meshes[i];
|
||||
uidScan.set(mesh.uid, true);
|
||||
if (uidToProxy.exists(mesh.uid)) continue;
|
||||
if (mesh.data == null || mesh.data.raw == null) continue;
|
||||
var raw = mesh.data.raw;
|
||||
if (raw.occluder_proxy == null) continue;
|
||||
var proxyRaw = raw.occluder_proxy;
|
||||
if (proxyRaw.positions == null || proxyRaw.indices == null) continue;
|
||||
var proxy = new OccluderProxy();
|
||||
proxy.setData(proxyRaw.positions, proxyRaw.indices, mesh.uid);
|
||||
uidToProxy.set(mesh.uid, proxy);
|
||||
}
|
||||
var dead: Array<Int> = null;
|
||||
for (uid in uidToProxy.keys()) {
|
||||
if (!uidScan.exists(uid)) {
|
||||
if (dead == null) dead = [];
|
||||
dead.push(uid);
|
||||
}
|
||||
}
|
||||
if (dead != null) for (uid in dead) uidToProxy.remove(uid);
|
||||
for (proxy in uidToProxy.iterator()) occluderProxies.push(proxy);
|
||||
}
|
||||
|
||||
public static function shutdown() {
|
||||
active = null;
|
||||
}
|
||||
}
|
||||
|
||||
class ShadowPassSlot {
|
||||
public var vp: Mat4 = null;
|
||||
public var rasterizer: SoftwareOcclusionRasterizer = null;
|
||||
public var sceneRev: Int = -1;
|
||||
public var appliedRev: Int = -1;
|
||||
public var lastUsed: Int = -1;
|
||||
public var flags: Array<Bool> = [];
|
||||
public var flagsLen: Int = 0;
|
||||
public function new() {}
|
||||
}
|
||||
9
leenkx/Sources/iron/culling/CullTier.hx
Normal file
9
leenkx/Sources/iron/culling/CullTier.hx
Normal file
@ -0,0 +1,9 @@
|
||||
package iron.culling;
|
||||
|
||||
enum CullTier {
|
||||
CPUSingleThread;
|
||||
CPUMultiThread;
|
||||
GPUCompute;
|
||||
GPUDriven;
|
||||
MultiGPU;
|
||||
}
|
||||
171
leenkx/Sources/iron/culling/HiZPyramid.hx
Normal file
171
leenkx/Sources/iron/culling/HiZPyramid.hx
Normal file
@ -0,0 +1,171 @@
|
||||
package iron.culling;
|
||||
|
||||
class HiZPyramid {
|
||||
|
||||
public var width: Int;
|
||||
public var height: Int;
|
||||
public var mipLevels: Int;
|
||||
public var mipBuffers: Array<kha.arrays.Float32Array>;
|
||||
public var mipWidths: Array<Int>;
|
||||
public var mipHeights: Array<Int>;
|
||||
|
||||
public var depthEps: Float;
|
||||
|
||||
var mipShifts: Array<Int>;
|
||||
|
||||
public function new(width: Int, height: Int) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
mipBuffers = [];
|
||||
mipWidths = [];
|
||||
mipHeights = [];
|
||||
depthEps = CullSystem.depthEps;
|
||||
|
||||
var w = width;
|
||||
var h = height;
|
||||
mipLevels = 0;
|
||||
while (w > 1 || h > 1) {
|
||||
mipWidths.push(w);
|
||||
mipHeights.push(h);
|
||||
mipBuffers.push(new kha.arrays.Float32Array(w * h));
|
||||
mipLevels++;
|
||||
w = (w + 1) >> 1;
|
||||
h = (h + 1) >> 1;
|
||||
}
|
||||
mipWidths.push(1);
|
||||
mipHeights.push(1);
|
||||
mipBuffers.push(new kha.arrays.Float32Array(1));
|
||||
mipLevels++;
|
||||
|
||||
mipShifts = [0, 1, 2, 3, 4, 5, 6];
|
||||
while (mipShifts.length < mipLevels) mipShifts.push(mipShifts.length);
|
||||
}
|
||||
|
||||
public function clear(farDepth: Float = 1e20) {
|
||||
var buf = mipBuffers[0];
|
||||
for (i in 0...buf.length) buf[i] = farDepth;
|
||||
}
|
||||
|
||||
public inline function writeDepth(x: Int, y: Int, depth: Float) {
|
||||
var idx = y * width + x;
|
||||
if (x >= 0 && x < width && y >= 0 && y < height) {
|
||||
if (depth < mipBuffers[0][idx]) {
|
||||
mipBuffers[0][idx] = depth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function buildPyramid() {
|
||||
for (mip in 1...mipLevels) {
|
||||
var prev = mipBuffers[mip - 1];
|
||||
var cur = mipBuffers[mip];
|
||||
var pw = mipWidths[mip - 1];
|
||||
var ph = mipHeights[mip - 1];
|
||||
var cw = mipWidths[mip];
|
||||
var ch = mipHeights[mip];
|
||||
|
||||
for (y in 0...ch) {
|
||||
for (x in 0...cw) {
|
||||
var sx = x * 2;
|
||||
var sy = y * 2;
|
||||
var d00 = prev[sy * pw + sx];
|
||||
var d10 = (sx + 1 < pw) ? prev[sy * pw + sx + 1] : d00;
|
||||
var d01 = (sy + 1 < ph) ? prev[(sy + 1) * pw + sx] : d00;
|
||||
var d11 = (sx + 1 < pw && sy + 1 < ph) ? prev[(sy + 1) * pw + sx + 1] : d00;
|
||||
var maxD = d00;
|
||||
if (d10 > maxD) maxD = d10;
|
||||
if (d01 > maxD) maxD = d01;
|
||||
if (d11 > maxD) maxD = d11;
|
||||
cur[y * cw + x] = maxD;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public inline function sample(x: Int, y: Int, mip: Int): Float {
|
||||
var mw = mipWidths[mip];
|
||||
var mh = mipHeights[mip];
|
||||
if (x < 0) x = 0;
|
||||
if (y < 0) y = 0;
|
||||
if (x >= mw) x = mw - 1;
|
||||
if (y >= mh) y = mh - 1;
|
||||
return mipBuffers[mip][y * mw + x];
|
||||
}
|
||||
|
||||
public static function mipForSize(rectW: Float, rectH: Float, maxMip: Int): Int {
|
||||
var maxDim = Math.max(rectW, rectH);
|
||||
if (maxDim <= 2.0) return 0;
|
||||
var mip = Std.int(Math.floor(Math.log(maxDim) / Math.log(2))) - 1;
|
||||
if (mip < 0) mip = 0;
|
||||
if (mip > maxMip) mip = maxMip;
|
||||
return mip;
|
||||
}
|
||||
|
||||
public function isOccluded(
|
||||
minX: Float, minY: Float, minZ: Float,
|
||||
maxX: Float, maxY: Float, maxZ: Float,
|
||||
vp: iron.math.Mat4,
|
||||
_screenW: Int, _screenH: Int
|
||||
): Bool {
|
||||
var minSx = 1e20, minSy = 1e20;
|
||||
var maxSx = -1e20, maxSy = -1e20;
|
||||
var minDepth = 1e20;
|
||||
|
||||
var m00 = vp._00, m10 = vp._10, m20 = vp._20, m30 = vp._30;
|
||||
var m01 = vp._01, m11 = vp._11, m21 = vp._21, m31 = vp._31;
|
||||
var m02 = vp._02, m12 = vp._12, m22 = vp._22, m32 = vp._32;
|
||||
var m03 = vp._03, m13 = vp._13, m23 = vp._23, m33 = vp._33;
|
||||
|
||||
for (c in 0...8) {
|
||||
var x = (c & 1) != 0 ? maxX : minX;
|
||||
var y = (c & 2) != 0 ? maxY : minY;
|
||||
var z = (c & 4) != 0 ? maxZ : minZ;
|
||||
var w = m03 * x + m13 * y + m23 * z + m33;
|
||||
if (w <= 0) return false;
|
||||
var invW = SimdOps.reciprocal(w);
|
||||
var ndcZ = (m02 * x + m12 * y + m22 * z + m32) * invW;
|
||||
if (ndcZ < -1.0) ndcZ = -1.0; else if (ndcZ > 1.0) ndcZ = 1.0;
|
||||
var sx = ((m00 * x + m10 * y + m20 * z + m30) * invW * 0.5 + 0.5) * width;
|
||||
var sy = ((m01 * x + m11 * y + m21 * z + m31) * invW * 0.5 + 0.5) * height;
|
||||
if (sx < minSx) minSx = sx;
|
||||
if (sy < minSy) minSy = sy;
|
||||
if (sx > maxSx) maxSx = sx;
|
||||
if (sy > maxSy) maxSy = sy;
|
||||
if (ndcZ < minDepth) minDepth = ndcZ;
|
||||
}
|
||||
|
||||
var ix0 = Std.int(Math.max(0, minSx));
|
||||
var iy0 = Std.int(Math.max(0, minSy));
|
||||
var ix1 = Std.int(Math.min(width - 1, maxSx));
|
||||
var iy1 = Std.int(Math.min(height - 1, maxSy));
|
||||
|
||||
if (ix0 > ix1 || iy0 > iy1) return false;
|
||||
|
||||
if (ix0 > 0) ix0--;
|
||||
if (iy0 > 0) iy0--;
|
||||
if (ix1 < width - 1) ix1++;
|
||||
if (iy1 < height - 1) iy1++;
|
||||
|
||||
var rectW = ix1 - ix0 + 1;
|
||||
var rectH = iy1 - iy0 + 1;
|
||||
var mip = mipForSize(rectW, rectH, mipLevels - 1);
|
||||
|
||||
var mw = mipWidths[mip];
|
||||
var mh = mipHeights[mip];
|
||||
var shift = mipShifts[mip];
|
||||
var tx0 = Std.int(Math.min(mw - 1, ix0 >> shift));
|
||||
var ty0 = Std.int(Math.min(mh - 1, iy0 >> shift));
|
||||
var tx1 = Std.int(Math.min(mw - 1, ix1 >> shift));
|
||||
var ty1 = Std.int(Math.min(mh - 1, iy1 >> shift));
|
||||
|
||||
var buf = mipBuffers[mip];
|
||||
var limit = minDepth - depthEps;
|
||||
for (ty in ty0...ty1 + 1) {
|
||||
var rowOff = ty * mw;
|
||||
for (tx in tx0...tx1 + 1) {
|
||||
if (buf[rowOff + tx] >= limit) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
60
leenkx/Sources/iron/culling/JobSystem.hx
Normal file
60
leenkx/Sources/iron/culling/JobSystem.hx
Normal file
@ -0,0 +1,60 @@
|
||||
package iron.culling;
|
||||
|
||||
#if (target.threaded)
|
||||
|
||||
class JobSystem {
|
||||
|
||||
public static var numWorkers(default, null): Int = 0;
|
||||
|
||||
static var jobQueue: sys.thread.Deque<Void->Void> = null;
|
||||
static var doneLock: sys.thread.Lock = null;
|
||||
static var jobError: Dynamic = null;
|
||||
|
||||
public static function init(workers: Int = 3) {
|
||||
if (jobQueue != null) return;
|
||||
numWorkers = workers < 1 ? 1 : workers;
|
||||
jobQueue = new sys.thread.Deque();
|
||||
doneLock = new sys.thread.Lock();
|
||||
for (_ in 0...numWorkers) {
|
||||
sys.thread.Thread.create(workerLoop);
|
||||
}
|
||||
}
|
||||
|
||||
static function workerLoop() {
|
||||
while (true) {
|
||||
var job = jobQueue.pop(true);
|
||||
if (job == null) return;
|
||||
try {
|
||||
job();
|
||||
} catch (e: Dynamic) {
|
||||
jobError = e;
|
||||
}
|
||||
doneLock.release();
|
||||
}
|
||||
}
|
||||
|
||||
public static function parallelFor(count: Int, fn: Int -> Int -> Void) {
|
||||
init();
|
||||
var chunks = numWorkers + 1;
|
||||
var chunkSize = Std.int((count + chunks - 1) / chunks);
|
||||
if (chunkSize < 1) chunkSize = 1;
|
||||
var jobs = 0;
|
||||
var start = chunkSize; // Chunk 0 runs on the calling thread
|
||||
while (start < count) {
|
||||
var s = start;
|
||||
var e = start + chunkSize > count ? count : start + chunkSize;
|
||||
jobQueue.add(function() fn(s, e));
|
||||
jobs++;
|
||||
start = e;
|
||||
}
|
||||
fn(0, chunkSize > count ? count : chunkSize);
|
||||
for (_ in 0...jobs) doneLock.wait();
|
||||
if (jobError != null) {
|
||||
var e = jobError;
|
||||
jobError = null;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#end
|
||||
376
leenkx/Sources/iron/culling/ObjectBVH.hx
Normal file
376
leenkx/Sources/iron/culling/ObjectBVH.hx
Normal file
@ -0,0 +1,376 @@
|
||||
package iron.culling;
|
||||
|
||||
class ObjectBVH {
|
||||
|
||||
static inline var LEAF_THRESHOLD = 4;
|
||||
static inline var NUM_BINS = 12;
|
||||
static inline var TRAVERSAL_COST = 1.0;
|
||||
static inline var INTERSECT_COST = 1.0;
|
||||
|
||||
public var nodes: Array<BVHNode>;
|
||||
public var objectIndices: Array<Int>;
|
||||
public var root: Int = -1;
|
||||
public var objectCount: Int = 0;
|
||||
|
||||
var stack: Array<Int> = [];
|
||||
|
||||
static var binCounts: Array<Int> = [for (i in 0...NUM_BINS) 0];
|
||||
static var binMinX: Array<Float> = [for (i in 0...NUM_BINS) 0];
|
||||
static var binMinY: Array<Float> = [for (i in 0...NUM_BINS) 0];
|
||||
static var binMinZ: Array<Float> = [for (i in 0...NUM_BINS) 0];
|
||||
static var binMaxX: Array<Float> = [for (i in 0...NUM_BINS) 0];
|
||||
static var binMaxY: Array<Float> = [for (i in 0...NUM_BINS) 0];
|
||||
static var binMaxZ: Array<Float> = [for (i in 0...NUM_BINS) 0];
|
||||
|
||||
public function new() {
|
||||
nodes = [];
|
||||
objectIndices = [];
|
||||
}
|
||||
|
||||
public function build(cullData: CullData) {
|
||||
objectCount = cullData.count;
|
||||
nodes = [];
|
||||
objectIndices = [];
|
||||
|
||||
if (objectCount == 0) {
|
||||
root = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
var indices = [for (i in 0...objectCount) i];
|
||||
root = buildRecursive(indices, cullData);
|
||||
}
|
||||
|
||||
static inline function surfaceArea(minX: Float, minY: Float, minZ: Float,
|
||||
maxX: Float, maxY: Float, maxZ: Float): Float {
|
||||
var wx = maxX - minX;
|
||||
var wy = maxY - minY;
|
||||
var wz = maxZ - minZ;
|
||||
return 2.0 * (wx * wy + wx * wz + wy * wz);
|
||||
}
|
||||
|
||||
function buildRecursive(indices: Array<Int>, cullData: CullData): Int {
|
||||
var nodeIdx = nodes.length;
|
||||
var node = new BVHNode();
|
||||
nodes.push(node);
|
||||
|
||||
var minX = 1e20, minY = 1e20, minZ = 1e20;
|
||||
var maxX = -1e20, maxY = -1e20, maxZ = -1e20;
|
||||
var centMinX = 1e20, centMinY = 1e20, centMinZ = 1e20;
|
||||
var centMaxX = -1e20, centMaxY = -1e20, centMaxZ = -1e20;
|
||||
var cx = 0.0, cy = 0.0, cz = 0.0;
|
||||
|
||||
for (idx in indices) {
|
||||
var x = cullData.getCenterX(idx);
|
||||
var y = cullData.getCenterY(idx);
|
||||
var z = cullData.getCenterZ(idx);
|
||||
if (cullData.getMinX(idx) < minX) minX = cullData.getMinX(idx);
|
||||
if (cullData.getMinY(idx) < minY) minY = cullData.getMinY(idx);
|
||||
if (cullData.getMinZ(idx) < minZ) minZ = cullData.getMinZ(idx);
|
||||
if (cullData.getMaxX(idx) > maxX) maxX = cullData.getMaxX(idx);
|
||||
if (cullData.getMaxY(idx) > maxY) maxY = cullData.getMaxY(idx);
|
||||
if (cullData.getMaxZ(idx) > maxZ) maxZ = cullData.getMaxZ(idx);
|
||||
if (x < centMinX) centMinX = x;
|
||||
if (y < centMinY) centMinY = y;
|
||||
if (z < centMinZ) centMinZ = z;
|
||||
if (x > centMaxX) centMaxX = x;
|
||||
if (y > centMaxY) centMaxY = y;
|
||||
if (z > centMaxZ) centMaxZ = z;
|
||||
cx += x;
|
||||
cy += y;
|
||||
cz += z;
|
||||
}
|
||||
|
||||
var n = indices.length;
|
||||
cx /= n;
|
||||
cy /= n;
|
||||
cz /= n;
|
||||
|
||||
node.minX = minX; node.minY = minY; node.minZ = minZ;
|
||||
node.maxX = maxX; node.maxY = maxY; node.maxZ = maxZ;
|
||||
node.centerX = cx;
|
||||
node.centerY = cy;
|
||||
node.centerZ = cz;
|
||||
|
||||
if (n <= LEAF_THRESHOLD) {
|
||||
node.isLeaf = true;
|
||||
node.firstObject = objectIndices.length;
|
||||
for (idx in indices) objectIndices.push(idx);
|
||||
node.objectCount = n;
|
||||
return nodeIdx;
|
||||
}
|
||||
|
||||
var parentSA = surfaceArea(minX, minY, minZ, maxX, maxY, maxZ);
|
||||
var leafCost = INTERSECT_COST * n;
|
||||
|
||||
var bestCost = 1e20;
|
||||
var bestAxis = -1;
|
||||
var bestSplitBin = -1;
|
||||
|
||||
var centBounds = [
|
||||
[centMinX, centMaxX],
|
||||
[centMinY, centMaxY],
|
||||
[centMinZ, centMaxZ]
|
||||
];
|
||||
|
||||
for (axis in 0...3) {
|
||||
var lo = centBounds[axis][0];
|
||||
var hi = centBounds[axis][1];
|
||||
var range = hi - lo;
|
||||
if (range < 1e-8) continue;
|
||||
|
||||
var invRange = NUM_BINS / range;
|
||||
|
||||
for (b in 0...NUM_BINS) {
|
||||
binCounts[b] = 0;
|
||||
binMinX[b] = 1e20; binMinY[b] = 1e20; binMinZ[b] = 1e20;
|
||||
binMaxX[b] = -1e20; binMaxY[b] = -1e20; binMaxZ[b] = -1e20;
|
||||
}
|
||||
|
||||
for (idx in indices) {
|
||||
var cv = axis == 0 ? cullData.getCenterX(idx) :
|
||||
axis == 1 ? cullData.getCenterY(idx) :
|
||||
cullData.getCenterZ(idx);
|
||||
var b = Std.int((cv - lo) * invRange);
|
||||
if (b < 0) b = 0;
|
||||
if (b >= NUM_BINS) b = NUM_BINS - 1;
|
||||
binCounts[b]++;
|
||||
if (cullData.getMinX(idx) < binMinX[b]) binMinX[b] = cullData.getMinX(idx);
|
||||
if (cullData.getMinY(idx) < binMinY[b]) binMinY[b] = cullData.getMinY(idx);
|
||||
if (cullData.getMinZ(idx) < binMinZ[b]) binMinZ[b] = cullData.getMinZ(idx);
|
||||
if (cullData.getMaxX(idx) > binMaxX[b]) binMaxX[b] = cullData.getMaxX(idx);
|
||||
if (cullData.getMaxY(idx) > binMaxY[b]) binMaxY[b] = cullData.getMaxY(idx);
|
||||
if (cullData.getMaxZ(idx) > binMaxZ[b]) binMaxZ[b] = cullData.getMaxZ(idx);
|
||||
}
|
||||
|
||||
var rightCount = [for (b in 0...NUM_BINS) 0];
|
||||
var rightMinX = [for (b in 0...NUM_BINS) 0.0];
|
||||
var rightMinY = [for (b in 0...NUM_BINS) 0.0];
|
||||
var rightMinZ = [for (b in 0...NUM_BINS) 0.0];
|
||||
var rightMaxX = [for (b in 0...NUM_BINS) 0.0];
|
||||
var rightMaxY = [for (b in 0...NUM_BINS) 0.0];
|
||||
var rightMaxZ = [for (b in 0...NUM_BINS) 0.0];
|
||||
|
||||
var cumCount = 0;
|
||||
var cumMinX = 1e20, cumMinY = 1e20, cumMinZ = 1e20;
|
||||
var cumMaxX = -1e20, cumMaxY = -1e20, cumMaxZ = -1e20;
|
||||
|
||||
for (b in 0...NUM_BINS) {
|
||||
var bi = NUM_BINS - 1 - b;
|
||||
cumCount += binCounts[bi];
|
||||
if (binMinX[bi] < cumMinX) cumMinX = binMinX[bi];
|
||||
if (binMinY[bi] < cumMinY) cumMinY = binMinY[bi];
|
||||
if (binMinZ[bi] < cumMinZ) cumMinZ = binMinZ[bi];
|
||||
if (binMaxX[bi] > cumMaxX) cumMaxX = binMaxX[bi];
|
||||
if (binMaxY[bi] > cumMaxY) cumMaxY = binMaxY[bi];
|
||||
if (binMaxZ[bi] > cumMaxZ) cumMaxZ = binMaxZ[bi];
|
||||
rightCount[bi] = cumCount;
|
||||
rightMinX[bi] = cumMinX; rightMinY[bi] = cumMinY; rightMinZ[bi] = cumMinZ;
|
||||
rightMaxX[bi] = cumMaxX; rightMaxY[bi] = cumMaxY; rightMaxZ[bi] = cumMaxZ;
|
||||
}
|
||||
|
||||
var leftCount = 0;
|
||||
var leftMinX = 1e20, leftMinY = 1e20, leftMinZ = 1e20;
|
||||
var leftMaxX = -1e20, leftMaxY = -1e20, leftMaxZ = -1e20;
|
||||
|
||||
for (b in 0...NUM_BINS - 1) {
|
||||
if (binCounts[b] == 0) continue;
|
||||
leftCount += binCounts[b];
|
||||
if (binMinX[b] < leftMinX) leftMinX = binMinX[b];
|
||||
if (binMinY[b] < leftMinY) leftMinY = binMinY[b];
|
||||
if (binMinZ[b] < leftMinZ) leftMinZ = binMinZ[b];
|
||||
if (binMaxX[b] > leftMaxX) leftMaxX = binMaxX[b];
|
||||
if (binMaxY[b] > leftMaxY) leftMaxY = binMaxY[b];
|
||||
if (binMaxZ[b] > leftMaxZ) leftMaxZ = binMaxZ[b];
|
||||
|
||||
var rc = rightCount[b + 1];
|
||||
if (rc == 0) continue;
|
||||
|
||||
var leftSA = surfaceArea(leftMinX, leftMinY, leftMinZ, leftMaxX, leftMaxY, leftMaxZ);
|
||||
var rightSA = surfaceArea(rightMinX[b + 1], rightMinY[b + 1], rightMinZ[b + 1],
|
||||
rightMaxX[b + 1], rightMaxY[b + 1], rightMaxZ[b + 1]);
|
||||
var cost = TRAVERSAL_COST + (leftSA * leftCount + rightSA * rc) / parentSA;
|
||||
|
||||
if (cost < bestCost) {
|
||||
bestCost = cost;
|
||||
bestAxis = axis;
|
||||
bestSplitBin = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestAxis < 0 || bestCost >= leafCost) {
|
||||
node.isLeaf = true;
|
||||
node.firstObject = objectIndices.length;
|
||||
for (idx in indices) objectIndices.push(idx);
|
||||
node.objectCount = n;
|
||||
return nodeIdx;
|
||||
}
|
||||
|
||||
var lo = centBounds[bestAxis][0];
|
||||
var hi = centBounds[bestAxis][1];
|
||||
var invRange = NUM_BINS / (hi - lo);
|
||||
var splitBin = bestSplitBin;
|
||||
|
||||
var left = [];
|
||||
var right = [];
|
||||
for (idx in indices) {
|
||||
var cv = bestAxis == 0 ? cullData.getCenterX(idx) :
|
||||
bestAxis == 1 ? cullData.getCenterY(idx) :
|
||||
cullData.getCenterZ(idx);
|
||||
var b = Std.int((cv - lo) * invRange);
|
||||
if (b < 0) b = 0;
|
||||
if (b >= NUM_BINS) b = NUM_BINS - 1;
|
||||
if (b <= splitBin) left.push(idx);
|
||||
else right.push(idx);
|
||||
}
|
||||
|
||||
if (left.length == 0 || right.length == 0) {
|
||||
indices.sort(function(a, b): Int {
|
||||
var av = bestAxis == 0 ? cullData.getCenterX(a) :
|
||||
bestAxis == 1 ? cullData.getCenterY(a) :
|
||||
cullData.getCenterZ(a);
|
||||
var bv = bestAxis == 0 ? cullData.getCenterX(b) :
|
||||
bestAxis == 1 ? cullData.getCenterY(b) :
|
||||
cullData.getCenterZ(b);
|
||||
if (av < bv) return -1;
|
||||
if (av > bv) return 1;
|
||||
return 0;
|
||||
});
|
||||
var mid = n >> 1;
|
||||
left = indices.slice(0, mid);
|
||||
right = indices.slice(mid);
|
||||
}
|
||||
|
||||
node.isLeaf = false;
|
||||
node.leftChild = buildRecursive(left, cullData);
|
||||
node.rightChild = buildRecursive(right, cullData);
|
||||
|
||||
return nodeIdx;
|
||||
}
|
||||
|
||||
|
||||
public function refit(cullData: CullData) {
|
||||
var i = nodes.length - 1;
|
||||
while (i >= 0) {
|
||||
var node = nodes[i];
|
||||
if (node.isLeaf) {
|
||||
var minX = 1e20, minY = 1e20, minZ = 1e20;
|
||||
var maxX = -1e20, maxY = -1e20, maxZ = -1e20;
|
||||
for (j in 0...node.objectCount) {
|
||||
var idx = objectIndices[node.firstObject + j];
|
||||
if (cullData.getMinX(idx) < minX) minX = cullData.getMinX(idx);
|
||||
if (cullData.getMinY(idx) < minY) minY = cullData.getMinY(idx);
|
||||
if (cullData.getMinZ(idx) < minZ) minZ = cullData.getMinZ(idx);
|
||||
if (cullData.getMaxX(idx) > maxX) maxX = cullData.getMaxX(idx);
|
||||
if (cullData.getMaxY(idx) > maxY) maxY = cullData.getMaxY(idx);
|
||||
if (cullData.getMaxZ(idx) > maxZ) maxZ = cullData.getMaxZ(idx);
|
||||
}
|
||||
node.minX = minX; node.minY = minY; node.minZ = minZ;
|
||||
node.maxX = maxX; node.maxY = maxY; node.maxZ = maxZ;
|
||||
} else {
|
||||
var l = nodes[node.leftChild];
|
||||
var r = nodes[node.rightChild];
|
||||
node.minX = l.minX < r.minX ? l.minX : r.minX;
|
||||
node.minY = l.minY < r.minY ? l.minY : r.minY;
|
||||
node.minZ = l.minZ < r.minZ ? l.minZ : r.minZ;
|
||||
node.maxX = l.maxX > r.maxX ? l.maxX : r.maxX;
|
||||
node.maxY = l.maxY > r.maxY ? l.maxY : r.maxY;
|
||||
node.maxZ = l.maxZ > r.maxZ ? l.maxZ : r.maxZ;
|
||||
}
|
||||
node.centerX = (node.minX + node.maxX) * 0.5;
|
||||
node.centerY = (node.minY + node.maxY) * 0.5;
|
||||
node.centerZ = (node.minZ + node.maxZ) * 0.5;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
public function forEachLeafInSubtree(node: BVHNode, cb: (Int) -> Void) {
|
||||
if (node.isLeaf) {
|
||||
for (i in 0...node.objectCount) {
|
||||
cb(objectIndices[node.firstObject + i]);
|
||||
}
|
||||
} else {
|
||||
forEachLeafInSubtree(nodes[node.leftChild], cb);
|
||||
forEachLeafInSubtree(nodes[node.rightChild], cb);
|
||||
}
|
||||
}
|
||||
|
||||
public function traverseFrontToBack(
|
||||
camX: Float, camY: Float, camZ: Float,
|
||||
shouldSkip: (BVHNode) -> Bool,
|
||||
onLeaf: (Int, Int) -> Void
|
||||
) {
|
||||
if (root < 0) return;
|
||||
|
||||
stack.resize(0);
|
||||
stack.push(root);
|
||||
|
||||
while (stack.length > 0) {
|
||||
var nodeIdx = stack.pop();
|
||||
var node = nodes[nodeIdx];
|
||||
|
||||
if (shouldSkip(node)) continue;
|
||||
|
||||
if (node.isLeaf) {
|
||||
var start = node.firstObject;
|
||||
for (i in 0...node.objectCount) {
|
||||
onLeaf(objectIndices[start + i], i);
|
||||
}
|
||||
} else {
|
||||
var leftNode = nodes[node.leftChild];
|
||||
var rightNode = nodes[node.rightChild];
|
||||
var leftDist = distSq(leftNode.centerX, leftNode.centerY, leftNode.centerZ, camX, camY, camZ);
|
||||
var rightDist = distSq(rightNode.centerX, rightNode.centerY, rightNode.centerZ, camX, camY, camZ);
|
||||
|
||||
if (leftDist > rightDist) {
|
||||
stack.push(node.leftChild);
|
||||
stack.push(node.rightChild);
|
||||
} else {
|
||||
stack.push(node.rightChild);
|
||||
stack.push(node.leftChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline function distSq(ax: Float, ay: Float, az: Float, bx: Float, by: Float, bz: Float): Float {
|
||||
var dx = ax - bx;
|
||||
var dy = ay - by;
|
||||
var dz = az - bz;
|
||||
return dx * dx + dy * dy + dz * dz;
|
||||
}
|
||||
|
||||
public static function aabbInFrustumRaw(
|
||||
minX: Float, minY: Float, minZ: Float,
|
||||
maxX: Float, maxY: Float, maxZ: Float,
|
||||
planes: Array<iron.object.FrustumPlane>
|
||||
): Bool {
|
||||
for (i in 0...planes.length) {
|
||||
var plane = planes[i];
|
||||
var px = plane.normal.x >= 0 ? maxX : minX;
|
||||
var py = plane.normal.y >= 0 ? maxY : minY;
|
||||
var pz = plane.normal.z >= 0 ? maxZ : minZ;
|
||||
if (SimdOps.dot3(px, py, pz, plane.normal.x, plane.normal.y, plane.normal.z) + plane.constant < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function aabbInFrustum(
|
||||
node: BVHNode,
|
||||
planes: Array<iron.object.FrustumPlane>
|
||||
): Bool {
|
||||
for (i in 0...planes.length) {
|
||||
var plane = planes[i];
|
||||
var px = plane.normal.x >= 0 ? node.maxX : node.minX;
|
||||
var py = plane.normal.y >= 0 ? node.maxY : node.minY;
|
||||
var pz = plane.normal.z >= 0 ? node.maxZ : node.minZ;
|
||||
if (SimdOps.dot3(px, py, pz, plane.normal.x, plane.normal.y, plane.normal.z) + plane.constant < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
71
leenkx/Sources/iron/culling/OccluderProxy.hx
Normal file
71
leenkx/Sources/iron/culling/OccluderProxy.hx
Normal file
@ -0,0 +1,71 @@
|
||||
package iron.culling;
|
||||
|
||||
class OccluderProxy {
|
||||
|
||||
public var positions: kha.arrays.Float32Array;
|
||||
public var indices: kha.arrays.Uint32Array;
|
||||
public var triangleCount: Int;
|
||||
public var meshUid: Int;
|
||||
|
||||
var worldPositions: kha.arrays.Float32Array;
|
||||
var mvp: iron.math.Mat4;
|
||||
|
||||
public function new() {
|
||||
indices = new kha.arrays.Uint32Array(0);
|
||||
triangleCount = 0;
|
||||
}
|
||||
|
||||
public function setData(pos: kha.arrays.Float32Array, idx: kha.arrays.Uint32Array, uid: Int) {
|
||||
#if js
|
||||
if (!Std.isOfType(pos, js.lib.DataView)) {
|
||||
var srcF: Array<Float> = cast pos;
|
||||
var dstF = new kha.arrays.Float32Array(srcF.length);
|
||||
for (i in 0...srcF.length) dstF[i] = srcF[i];
|
||||
pos = dstF;
|
||||
}
|
||||
if (!Std.isOfType(idx, js.lib.DataView)) {
|
||||
var srcI: Array<Int> = cast idx;
|
||||
var dstI = new kha.arrays.Uint32Array(srcI.length);
|
||||
for (i in 0...srcI.length) dstI[i] = srcI[i];
|
||||
idx = dstI;
|
||||
}
|
||||
#end
|
||||
positions = pos;
|
||||
indices = idx;
|
||||
triangleCount = Std.int(idx.length / 3);
|
||||
meshUid = uid;
|
||||
}
|
||||
|
||||
public function rasterize(
|
||||
rasterizer: SoftwareOcclusionRasterizer,
|
||||
world: iron.math.Mat4,
|
||||
vp: iron.math.Mat4,
|
||||
screenW: Int, screenH: Int
|
||||
) {
|
||||
if (positions == null || indices == null || triangleCount == 0) return;
|
||||
|
||||
var vertCount = Std.int(positions.length / 3);
|
||||
|
||||
var clipSize = vertCount * 4;
|
||||
if (worldPositions == null || worldPositions.length < clipSize) {
|
||||
worldPositions = new kha.arrays.Float32Array(clipSize);
|
||||
}
|
||||
|
||||
if (mvp == null) mvp = iron.math.Mat4.identity();
|
||||
mvp.multmats(vp, world);
|
||||
|
||||
for (i in 0...vertCount) {
|
||||
var x = positions[i * 3];
|
||||
var y = positions[i * 3 + 1];
|
||||
var z = positions[i * 3 + 2];
|
||||
var off = i * 4;
|
||||
worldPositions[off] = mvp._00 * x + mvp._10 * y + mvp._20 * z + mvp._30;
|
||||
worldPositions[off + 1] = mvp._01 * x + mvp._11 * y + mvp._21 * z + mvp._31;
|
||||
worldPositions[off + 2] = mvp._02 * x + mvp._12 * y + mvp._22 * z + mvp._32;
|
||||
worldPositions[off + 3] = mvp._03 * x + mvp._13 * y + mvp._23 * z + mvp._33;
|
||||
}
|
||||
|
||||
rasterizer.rasterizeProxyTransformed(worldPositions, indices, screenW, screenH);
|
||||
}
|
||||
|
||||
}
|
||||
83
leenkx/Sources/iron/culling/SimdOps.hx
Normal file
83
leenkx/Sources/iron/culling/SimdOps.hx
Normal file
@ -0,0 +1,83 @@
|
||||
package iron.culling;
|
||||
|
||||
#if cpp
|
||||
@:include("smmintrin.h")
|
||||
@:include("emmintrin.h")
|
||||
@:include("xmmintrin.h")
|
||||
#end
|
||||
class SimdOps {
|
||||
|
||||
public static inline function dot4(
|
||||
a0: Float, a1: Float, a2: Float, a3: Float,
|
||||
b0: Float, b1: Float, b2: Float, b3: Float
|
||||
): Float {
|
||||
#if cpp
|
||||
return untyped __cpp__("_mm_cvtss_f32(_mm_dp_ps(_mm_setr_ps({0}, {1}, {2}, {3}), _mm_setr_ps({4}, {5}, {6}, {7}), 0xF1))",
|
||||
a0, a1, a2, a3, b0, b1, b2, b3);
|
||||
#else
|
||||
return a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
|
||||
#end
|
||||
}
|
||||
|
||||
public static inline function dot3(
|
||||
a0: Float, a1: Float, a2: Float,
|
||||
b0: Float, b1: Float, b2: Float
|
||||
): Float {
|
||||
#if cpp
|
||||
return untyped __cpp__("_mm_cvtss_f32(_mm_dp_ps(_mm_setr_ps({0}, {1}, {2}, 0.0f), _mm_setr_ps({3}, {4}, {5}, 0.0f), 0x71))",
|
||||
a0, a1, a2, b0, b1, b2);
|
||||
#else
|
||||
return a0 * b0 + a1 * b1 + a2 * b2;
|
||||
#end
|
||||
}
|
||||
|
||||
public static inline function reciprocal(x: Float): Float {
|
||||
#if cpp
|
||||
return untyped __cpp__("({ auto _r = _mm_rcp_ss(_mm_set_ss({0})); _mm_cvtss_f32(_mm_add_ss(_mm_mul_ss(_r, _mm_sub_ss(_mm_set_ss(2.0f), _mm_mul_ss(_r, _mm_set_ss({0})))), _r)); })", x);
|
||||
#else
|
||||
return 1.0 / x;
|
||||
#end
|
||||
}
|
||||
|
||||
public static inline function max4f(
|
||||
a: Float, b: Float, c: Float, d: Float
|
||||
): Float {
|
||||
#if cpp
|
||||
return untyped __cpp__("_mm_cvtss_f32(_mm_max_ss(_mm_max_ss(_mm_set_ss({0}), _mm_set_ss({1})), _mm_max_ss(_mm_set_ss({2}), _mm_set_ss({3}))))",
|
||||
a, b, c, d);
|
||||
#else
|
||||
var m = a;
|
||||
if (b > m) m = b;
|
||||
if (c > m) m = c;
|
||||
if (d > m) m = d;
|
||||
return m;
|
||||
#end
|
||||
}
|
||||
|
||||
public static inline function min4f(
|
||||
a: Float, b: Float, c: Float, d: Float
|
||||
): Float {
|
||||
#if cpp
|
||||
return untyped __cpp__("_mm_cvtss_f32(_mm_min_ss(_mm_min_ss(_mm_set_ss({0}), _mm_set_ss({1})), _mm_min_ss(_mm_set_ss({2}), _mm_set_ss({3}))))",
|
||||
a, b, c, d);
|
||||
#else
|
||||
var m = a;
|
||||
if (b < m) m = b;
|
||||
if (c < m) m = c;
|
||||
if (d < m) m = d;
|
||||
return m;
|
||||
#end
|
||||
}
|
||||
|
||||
public static inline function frustumSphereTest6(
|
||||
planeData: kha.arrays.Float32Array,
|
||||
cx: Float, cy: Float, cz: Float, radius: Float
|
||||
): Bool {
|
||||
for (i in 0...6) {
|
||||
var base = i * 4;
|
||||
var dist = dot3(cx, cy, cz, planeData[base], planeData[base + 1], planeData[base + 2]) + planeData[base + 3];
|
||||
if (dist + radius < 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
507
leenkx/Sources/iron/culling/SoftwareOcclusionRasterizer.hx
Normal file
507
leenkx/Sources/iron/culling/SoftwareOcclusionRasterizer.hx
Normal file
@ -0,0 +1,507 @@
|
||||
package iron.culling;
|
||||
|
||||
class SoftwareOcclusionRasterizer {
|
||||
|
||||
public var hiz: HiZPyramid;
|
||||
public var width: Int;
|
||||
public var height: Int;
|
||||
|
||||
public var nearClip: Float = 0.0001;
|
||||
|
||||
static inline var TILE_SIZE = 32;
|
||||
|
||||
public var trianglesRasterized: Int = 0;
|
||||
|
||||
public var collectMode = false;
|
||||
var screenTris: Array<Float> = [];
|
||||
var screenTriCount = 0;
|
||||
|
||||
static final BOX_FACES = [
|
||||
[0, 1, 3, 2], // -Z
|
||||
[4, 5, 7, 6], // +Z
|
||||
[0, 1, 5, 4], // -Y
|
||||
[2, 3, 7, 6], // +Y
|
||||
[0, 2, 6, 4], // -X
|
||||
[1, 3, 7, 5], // +X
|
||||
];
|
||||
|
||||
var inX: Array<Float> = [0,0,0];
|
||||
var inY: Array<Float> = [0,0,0];
|
||||
var inZ: Array<Float> = [0,0,0];
|
||||
var inW: Array<Float> = [0,0,0];
|
||||
var outX: Array<Float> = [0,0,0,0];
|
||||
var outY: Array<Float> = [0,0,0,0];
|
||||
var outZ: Array<Float> = [0,0,0,0];
|
||||
var outW: Array<Float> = [0,0,0,0];
|
||||
var projSx: Array<Float> = [0,0,0,0];
|
||||
var projSy: Array<Float> = [0,0,0,0];
|
||||
var projSz: Array<Float> = [0,0,0,0];
|
||||
|
||||
var boxClipX: Array<Float> = [0,0,0,0,0,0,0,0];
|
||||
var boxClipY: Array<Float> = [0,0,0,0,0,0,0,0];
|
||||
var boxClipZ: Array<Float> = [0,0,0,0,0,0,0,0];
|
||||
var boxClipW: Array<Float> = [0,0,0,0,0,0,0,0];
|
||||
|
||||
public function new(width: Int = 256, height: Int = 128) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
hiz = new HiZPyramid(width, height);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
hiz.clear(1e20);
|
||||
trianglesRasterized = 0;
|
||||
}
|
||||
|
||||
public function rasterizeTriangle(
|
||||
v0x: Float, v0y: Float, v0z: Float,
|
||||
v1x: Float, v1y: Float, v1z: Float,
|
||||
v2x: Float, v2y: Float, v2z: Float
|
||||
) {
|
||||
var e0a = v1y - v0y; var e0b = v0x - v1x;
|
||||
var e1a = v2y - v1y; var e1b = v1x - v2x;
|
||||
var e2a = v0y - v2y; var e2b = v2x - v0x;
|
||||
var e0c = -e0a * v0x - e0b * v0y;
|
||||
var e1c = -e1a * v1x - e1b * v1y;
|
||||
var e2c = -e2a * v2x - e2b * v2y;
|
||||
|
||||
var area = e0a * v2x + e0b * v2y + e0c;
|
||||
if (area < 0) {
|
||||
area = -area;
|
||||
e0a = -e0a; e0b = -e0b; e0c = -e0c;
|
||||
e1a = -e1a; e1b = -e1b; e1c = -e1c;
|
||||
e2a = -e2a; e2b = -e2b; e2c = -e2c;
|
||||
}
|
||||
if (area < 1e-8) return;
|
||||
|
||||
var minX = Std.int(Math.max(0, Math.floor(Math.min(v0x, Math.min(v1x, v2x)))));
|
||||
var maxX = Std.int(Math.min(width - 1, Math.ceil(Math.max(v0x, Math.max(v1x, v2x)))));
|
||||
var minY = Std.int(Math.max(0, Math.floor(Math.min(v0y, Math.min(v1y, v2y)))));
|
||||
var maxY = Std.int(Math.min(height - 1, Math.ceil(Math.max(v0y, Math.max(v1y, v2y)))));
|
||||
if (minX > maxX || minY > maxY) return;
|
||||
|
||||
var d = (v1x - v0x) * (v2y - v0y) - (v2x - v0x) * (v1y - v0y);
|
||||
var invD = 1.0 / d;
|
||||
var dzdx = ((v1z - v0z) * (v2y - v0y) - (v2z - v0z) * (v1y - v0y)) * invD;
|
||||
var dzdy = ((v2z - v0z) * (v1x - v0x) - (v1z - v0z) * (v2x - v0x)) * invD;
|
||||
|
||||
var zPush = (Math.abs(dzdx) + Math.abs(dzdy)) * 0.5;
|
||||
var zMaxTri = v0z;
|
||||
if (v1z > zMaxTri) zMaxTri = v1z;
|
||||
if (v2z > zMaxTri) zMaxTri = v2z;
|
||||
|
||||
var cx0 = minX + 0.5;
|
||||
var cy0 = minY + 0.5;
|
||||
var e0row = e0a * cx0 + e0b * cy0 + e0c;
|
||||
var e1row = e1a * cx0 + e1b * cy0 + e1c;
|
||||
var e2row = e2a * cx0 + e2b * cy0 + e2c;
|
||||
var zrow = v0z + dzdx * (cx0 - v0x) + dzdy * (cy0 - v0y);
|
||||
|
||||
var buf = hiz.mipBuffers[0];
|
||||
var w = width;
|
||||
for (py in minY...maxY + 1) {
|
||||
var e0 = e0row; var e1 = e1row; var e2 = e2row;
|
||||
var z = zrow;
|
||||
var idx = py * w + minX;
|
||||
for (px in minX...maxX + 1) {
|
||||
if (e0 >= 0 && e1 >= 0 && e2 >= 0) {
|
||||
var zPix = z + zPush;
|
||||
if (zPix > zMaxTri) zPix = zMaxTri;
|
||||
if (zPix < 0.0) zPix = 0.0;
|
||||
if (zPix > 1.0) zPix = 1.0;
|
||||
if (zPix < buf[idx]) buf[idx] = zPix;
|
||||
}
|
||||
e0 += e0a; e1 += e1a; e2 += e2a;
|
||||
z += dzdx;
|
||||
idx++;
|
||||
}
|
||||
e0row += e0b; e1row += e1b; e2row += e2b;
|
||||
zrow += dzdy;
|
||||
}
|
||||
}
|
||||
|
||||
function clipNearAndRasterize(
|
||||
cx0: Float, cy0: Float, cz0: Float, cw0: Float,
|
||||
cx1: Float, cy1: Float, cz1: Float, cw1: Float,
|
||||
cx2: Float, cy2: Float, cz2: Float, cw2: Float
|
||||
) {
|
||||
inX[0] = cx0; inY[0] = cy0; inZ[0] = cz0; inW[0] = cw0;
|
||||
inX[1] = cx1; inY[1] = cy1; inZ[1] = cz1; inW[1] = cw1;
|
||||
inX[2] = cx2; inY[2] = cy2; inZ[2] = cz2; inW[2] = cw2;
|
||||
|
||||
var outCount = 0;
|
||||
for (i in 0...3) {
|
||||
var aIdx = (i + 2) % 3;
|
||||
var bIdx = i;
|
||||
var aInside = inW[aIdx] >= nearClip;
|
||||
var bInside = inW[bIdx] >= nearClip;
|
||||
|
||||
if (aInside) {
|
||||
if (bInside) {
|
||||
outX[outCount] = inX[bIdx]; outY[outCount] = inY[bIdx];
|
||||
outZ[outCount] = inZ[bIdx]; outW[outCount] = inW[bIdx];
|
||||
outCount++;
|
||||
} else {
|
||||
var t = (inW[aIdx] - nearClip) / (inW[aIdx] - inW[bIdx]);
|
||||
outX[outCount] = inX[aIdx] + t * (inX[bIdx] - inX[aIdx]);
|
||||
outY[outCount] = inY[aIdx] + t * (inY[bIdx] - inY[aIdx]);
|
||||
outZ[outCount] = inZ[aIdx] + t * (inZ[bIdx] - inZ[aIdx]);
|
||||
outW[outCount] = inW[aIdx] + t * (inW[bIdx] - inW[aIdx]);
|
||||
outCount++;
|
||||
}
|
||||
} else {
|
||||
if (bInside) {
|
||||
var t = (inW[aIdx] - nearClip) / (inW[aIdx] - inW[bIdx]);
|
||||
outX[outCount] = inX[aIdx] + t * (inX[bIdx] - inX[aIdx]);
|
||||
outY[outCount] = inY[aIdx] + t * (inY[bIdx] - inY[aIdx]);
|
||||
outZ[outCount] = inZ[aIdx] + t * (inZ[bIdx] - inZ[aIdx]);
|
||||
outW[outCount] = inW[aIdx] + t * (inW[bIdx] - inW[aIdx]);
|
||||
outCount++;
|
||||
outX[outCount] = inX[bIdx]; outY[outCount] = inY[bIdx];
|
||||
outZ[outCount] = inZ[bIdx]; outW[outCount] = inW[bIdx];
|
||||
outCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (outCount < 3) return;
|
||||
|
||||
for (i in 0...outCount) {
|
||||
var invW = SimdOps.reciprocal(outW[i]);
|
||||
projSx[i] = (outX[i] * invW * 0.5 + 0.5) * width;
|
||||
projSy[i] = (outY[i] * invW * 0.5 + 0.5) * height;
|
||||
projSz[i] = outZ[i] * invW;
|
||||
}
|
||||
|
||||
var signedArea = (projSx[1] - projSx[0]) * (projSy[2] - projSy[0]) -
|
||||
(projSy[1] - projSy[0]) * (projSx[2] - projSx[0]);
|
||||
if (Math.abs(signedArea) < 1e-8) return;
|
||||
|
||||
trianglesRasterized++;
|
||||
|
||||
if (collectMode) {
|
||||
screenTris[screenTriCount++] = projSx[0]; screenTris[screenTriCount++] = projSy[0]; screenTris[screenTriCount++] = projSz[0];
|
||||
screenTris[screenTriCount++] = projSx[1]; screenTris[screenTriCount++] = projSy[1]; screenTris[screenTriCount++] = projSz[1];
|
||||
screenTris[screenTriCount++] = projSx[2]; screenTris[screenTriCount++] = projSy[2]; screenTris[screenTriCount++] = projSz[2];
|
||||
if (outCount == 4) {
|
||||
screenTris[screenTriCount++] = projSx[0]; screenTris[screenTriCount++] = projSy[0]; screenTris[screenTriCount++] = projSz[0];
|
||||
screenTris[screenTriCount++] = projSx[2]; screenTris[screenTriCount++] = projSy[2]; screenTris[screenTriCount++] = projSz[2];
|
||||
screenTris[screenTriCount++] = projSx[3]; screenTris[screenTriCount++] = projSy[3]; screenTris[screenTriCount++] = projSz[3];
|
||||
}
|
||||
} else {
|
||||
rasterizeTriangle(
|
||||
projSx[0], projSy[0], projSz[0],
|
||||
projSx[1], projSy[1], projSz[1],
|
||||
projSx[2], projSy[2], projSz[2]
|
||||
);
|
||||
if (outCount == 4) {
|
||||
rasterizeTriangle(
|
||||
projSx[0], projSy[0], projSz[0],
|
||||
projSx[2], projSy[2], projSz[2],
|
||||
projSx[3], projSy[3], projSz[3]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function rasterizeProxy(
|
||||
positions: kha.arrays.Float32Array,
|
||||
indices: kha.arrays.Uint32Array,
|
||||
vp: iron.math.Mat4,
|
||||
screenW: Int, screenH: Int
|
||||
) {
|
||||
var triCount = Std.int(indices.length / 3);
|
||||
|
||||
var m00 = vp._00, m10 = vp._10, m20 = vp._20, m30 = vp._30;
|
||||
var m01 = vp._01, m11 = vp._11, m21 = vp._21, m31 = vp._31;
|
||||
var m02 = vp._02, m12 = vp._12, m22 = vp._22, m32 = vp._32;
|
||||
var m03 = vp._03, m13 = vp._13, m23 = vp._23, m33 = vp._33;
|
||||
|
||||
for (t in 0...triCount) {
|
||||
var i0 = indices[t * 3] * 3;
|
||||
var i1 = indices[t * 3 + 1] * 3;
|
||||
var i2 = indices[t * 3 + 2] * 3;
|
||||
|
||||
var px0 = positions[i0], py0 = positions[i0 + 1], pz0 = positions[i0 + 2];
|
||||
var px1 = positions[i1], py1 = positions[i1 + 1], pz1 = positions[i1 + 2];
|
||||
var px2 = positions[i2], py2 = positions[i2 + 1], pz2 = positions[i2 + 2];
|
||||
|
||||
var cw0 = m03 * px0 + m13 * py0 + m23 * pz0 + m33;
|
||||
var cw1 = m03 * px1 + m13 * py1 + m23 * pz1 + m33;
|
||||
var cw2 = m03 * px2 + m13 * py2 + m23 * pz2 + m33;
|
||||
|
||||
if (cw0 < nearClip && cw1 < nearClip && cw2 < nearClip) continue;
|
||||
|
||||
var cx0 = m00 * px0 + m10 * py0 + m20 * pz0 + m30;
|
||||
var cy0 = m01 * px0 + m11 * py0 + m21 * pz0 + m31;
|
||||
var cz0 = m02 * px0 + m12 * py0 + m22 * pz0 + m32;
|
||||
|
||||
var cx1 = m00 * px1 + m10 * py1 + m20 * pz1 + m30;
|
||||
var cy1 = m01 * px1 + m11 * py1 + m21 * pz1 + m31;
|
||||
var cz1 = m02 * px1 + m12 * py1 + m22 * pz1 + m32;
|
||||
|
||||
var cx2 = m00 * px2 + m10 * py2 + m20 * pz2 + m30;
|
||||
var cy2 = m01 * px2 + m11 * py2 + m21 * pz2 + m31;
|
||||
var cz2 = m02 * px2 + m12 * py2 + m22 * pz2 + m32;
|
||||
|
||||
clipNearAndRasterize(
|
||||
cx0, cy0, cz0, cw0,
|
||||
cx1, cy1, cz1, cw1,
|
||||
cx2, cy2, cz2, cw2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function rasterizeProxyTransformed(
|
||||
clipPositions: kha.arrays.Float32Array,
|
||||
indices: kha.arrays.Uint32Array,
|
||||
screenW: Int, screenH: Int
|
||||
) {
|
||||
var triCount = Std.int(indices.length / 3);
|
||||
|
||||
for (t in 0...triCount) {
|
||||
var i0 = indices[t * 3] * 4;
|
||||
var i1 = indices[t * 3 + 1] * 4;
|
||||
var i2 = indices[t * 3 + 2] * 4;
|
||||
|
||||
var cx0 = clipPositions[i0], cy0 = clipPositions[i0 + 1], cz0 = clipPositions[i0 + 2], cw0 = clipPositions[i0 + 3];
|
||||
var cx1 = clipPositions[i1], cy1 = clipPositions[i1 + 1], cz1 = clipPositions[i1 + 2], cw1 = clipPositions[i1 + 3];
|
||||
var cx2 = clipPositions[i2], cy2 = clipPositions[i2 + 1], cz2 = clipPositions[i2 + 2], cw2 = clipPositions[i2 + 3];
|
||||
|
||||
if (cw0 < nearClip && cw1 < nearClip && cw2 < nearClip) continue;
|
||||
|
||||
clipNearAndRasterize(
|
||||
cx0, cy0, cz0, cw0,
|
||||
cx1, cy1, cz1, cw1,
|
||||
cx2, cy2, cz2, cw2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function rasterizeBox(
|
||||
minX: Float, minY: Float, minZ: Float,
|
||||
maxX: Float, maxY: Float, maxZ: Float,
|
||||
vp: iron.math.Mat4,
|
||||
screenW: Int, screenH: Int
|
||||
) {
|
||||
var m00 = vp._00, m10 = vp._10, m20 = vp._20, m30 = vp._30;
|
||||
var m01 = vp._01, m11 = vp._11, m21 = vp._21, m31 = vp._31;
|
||||
var m02 = vp._02, m12 = vp._12, m22 = vp._22, m32 = vp._32;
|
||||
var m03 = vp._03, m13 = vp._13, m23 = vp._23, m33 = vp._33;
|
||||
|
||||
for (c in 0...8) {
|
||||
var x = (c & 1) != 0 ? maxX : minX;
|
||||
var y = (c & 2) != 0 ? maxY : minY;
|
||||
var z = (c & 4) != 0 ? maxZ : minZ;
|
||||
boxClipX[c] = m00 * x + m10 * y + m20 * z + m30;
|
||||
boxClipY[c] = m01 * x + m11 * y + m21 * z + m31;
|
||||
boxClipZ[c] = m02 * x + m12 * y + m22 * z + m32;
|
||||
boxClipW[c] = m03 * x + m13 * y + m23 * z + m33;
|
||||
}
|
||||
|
||||
for (face in BOX_FACES) {
|
||||
clipNearAndRasterize(
|
||||
boxClipX[face[0]], boxClipY[face[0]], boxClipZ[face[0]], boxClipW[face[0]],
|
||||
boxClipX[face[1]], boxClipY[face[1]], boxClipZ[face[1]], boxClipW[face[1]],
|
||||
boxClipX[face[2]], boxClipY[face[2]], boxClipZ[face[2]], boxClipW[face[2]]
|
||||
);
|
||||
clipNearAndRasterize(
|
||||
boxClipX[face[0]], boxClipY[face[0]], boxClipZ[face[0]], boxClipW[face[0]],
|
||||
boxClipX[face[2]], boxClipY[face[2]], boxClipZ[face[2]], boxClipW[face[2]],
|
||||
boxClipX[face[3]], boxClipY[face[3]], boxClipZ[face[3]], boxClipW[face[3]]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function buildHiZ() {
|
||||
hiz.buildPyramid();
|
||||
}
|
||||
|
||||
public function beginCollect() {
|
||||
collectMode = true;
|
||||
screenTriCount = 0;
|
||||
}
|
||||
|
||||
var tileTriCounts: Array<Int> = [];
|
||||
var tileTriOffsets: Array<Int> = [];
|
||||
var tileTriLists: Array<Array<Int>> = [];
|
||||
|
||||
public function flushCollected() {
|
||||
collectMode = false;
|
||||
var nTris = Std.int(screenTriCount / 9);
|
||||
if (nTris == 0) return;
|
||||
|
||||
var tilesX = Std.int(Math.ceil(width / TILE_SIZE));
|
||||
var tilesY = Std.int(Math.ceil(height / TILE_SIZE));
|
||||
var totalTiles = tilesX * tilesY;
|
||||
|
||||
if (tileTriCounts.length < totalTiles) {
|
||||
tileTriCounts.resize(totalTiles);
|
||||
tileTriOffsets.resize(totalTiles);
|
||||
tileTriLists.resize(totalTiles);
|
||||
}
|
||||
for (t in 0...totalTiles) tileTriCounts[t] = 0;
|
||||
for (t in 0...nTris) {
|
||||
var off = t * 9;
|
||||
var tx0 = Std.int(Math.max(0, Math.floor(Math.min(
|
||||
Math.min(screenTris[off], screenTris[off + 3]), screenTris[off + 6]) / TILE_SIZE)));
|
||||
var tx1 = Std.int(Math.min(tilesX - 1, Math.ceil(Math.max(
|
||||
Math.max(screenTris[off], screenTris[off + 3]), screenTris[off + 6]) / TILE_SIZE)));
|
||||
var ty0 = Std.int(Math.max(0, Math.floor(Math.min(
|
||||
Math.min(screenTris[off + 1], screenTris[off + 4]), screenTris[off + 7]) / TILE_SIZE)));
|
||||
var ty1 = Std.int(Math.min(tilesY - 1, Math.ceil(Math.max(
|
||||
Math.max(screenTris[off + 1], screenTris[off + 4]), screenTris[off + 7]) / TILE_SIZE)));
|
||||
for (ty in ty0...ty1 + 1) {
|
||||
for (tx in tx0...tx1 + 1) {
|
||||
tileTriCounts[ty * tilesX + tx]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (t in 0...totalTiles) {
|
||||
var l = tileTriLists[t];
|
||||
if (l == null) { l = []; tileTriLists[t] = l; }
|
||||
if (l.length < tileTriCounts[t]) l.resize(tileTriCounts[t]);
|
||||
tileTriOffsets[t] = 0;
|
||||
}
|
||||
for (t in 0...nTris) {
|
||||
var off = t * 9;
|
||||
var tx0 = Std.int(Math.max(0, Math.floor(Math.min(
|
||||
Math.min(screenTris[off], screenTris[off + 3]), screenTris[off + 6]) / TILE_SIZE)));
|
||||
var tx1 = Std.int(Math.min(tilesX - 1, Math.ceil(Math.max(
|
||||
Math.max(screenTris[off], screenTris[off + 3]), screenTris[off + 6]) / TILE_SIZE)));
|
||||
var ty0 = Std.int(Math.max(0, Math.floor(Math.min(
|
||||
Math.min(screenTris[off + 1], screenTris[off + 4]), screenTris[off + 7]) / TILE_SIZE)));
|
||||
var ty1 = Std.int(Math.min(tilesY - 1, Math.ceil(Math.max(
|
||||
Math.max(screenTris[off + 1], screenTris[off + 4]), screenTris[off + 7]) / TILE_SIZE)));
|
||||
for (ty in ty0...ty1 + 1) {
|
||||
for (tx in tx0...tx1 + 1) {
|
||||
var ti = ty * tilesX + tx;
|
||||
tileTriLists[ti][tileTriOffsets[ti]++] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var buf = hiz.mipBuffers[0];
|
||||
var w = width;
|
||||
var tris = screenTris;
|
||||
|
||||
#if (target.threaded)
|
||||
JobSystem.parallelFor(totalTiles, function(start, end) {
|
||||
for (ti in start...end) {
|
||||
var tx = ti % tilesX;
|
||||
var ty = Std.int(ti / tilesX);
|
||||
var px0 = tx * TILE_SIZE;
|
||||
var py0 = ty * TILE_SIZE;
|
||||
var px1 = Std.int(Math.min(w - 1, px0 + TILE_SIZE - 1));
|
||||
var py1 = Std.int(Math.min(height - 1, py0 + TILE_SIZE - 1));
|
||||
var list = tileTriLists[ti];
|
||||
var listLen = tileTriCounts[ti];
|
||||
for (li in 0...listLen) {
|
||||
var off = list[li] * 9;
|
||||
rasterizeTriangleBounded(
|
||||
tris[off], tris[off + 1], tris[off + 2],
|
||||
tris[off + 3], tris[off + 4], tris[off + 5],
|
||||
tris[off + 6], tris[off + 7], tris[off + 8],
|
||||
px0, py0, px1, py1, buf, w
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
#else
|
||||
for (ti in 0...totalTiles) {
|
||||
var tx = ti % tilesX;
|
||||
var ty = Std.int(ti / tilesX);
|
||||
var px0 = tx * TILE_SIZE;
|
||||
var py0 = ty * TILE_SIZE;
|
||||
var px1 = Std.int(Math.min(w - 1, px0 + TILE_SIZE - 1));
|
||||
var py1 = Std.int(Math.min(height - 1, py0 + TILE_SIZE - 1));
|
||||
var list = tileTriLists[ti];
|
||||
for (li in 0...list.length) {
|
||||
var off = list[li] * 9;
|
||||
rasterizeTriangleBounded(
|
||||
tris[off], tris[off + 1], tris[off + 2],
|
||||
tris[off + 3], tris[off + 4], tris[off + 5],
|
||||
tris[off + 6], tris[off + 7], tris[off + 8],
|
||||
px0, py0, px1, py1, buf, w
|
||||
);
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
function rasterizeTriangleBounded(
|
||||
v0x: Float, v0y: Float, v0z: Float,
|
||||
v1x: Float, v1y: Float, v1z: Float,
|
||||
v2x: Float, v2y: Float, v2z: Float,
|
||||
tileX0: Int, tileY0: Int, tileX1: Int, tileY1: Int,
|
||||
buf: kha.arrays.Float32Array, w: Int
|
||||
) {
|
||||
var e0a = v1y - v0y; var e0b = v0x - v1x;
|
||||
var e1a = v2y - v1y; var e1b = v1x - v2x;
|
||||
var e2a = v0y - v2y; var e2b = v2x - v0x;
|
||||
var e0c = -e0a * v0x - e0b * v0y;
|
||||
var e1c = -e1a * v1x - e1b * v1y;
|
||||
var e2c = -e2a * v2x - e2b * v2y;
|
||||
|
||||
var area = e0a * v2x + e0b * v2y + e0c;
|
||||
if (area < 0) {
|
||||
area = -area;
|
||||
e0a = -e0a; e0b = -e0b; e0c = -e0c;
|
||||
e1a = -e1a; e1b = -e1b; e1c = -e1c;
|
||||
e2a = -e2a; e2b = -e2b; e2c = -e2c;
|
||||
}
|
||||
if (area < 1e-8) return;
|
||||
|
||||
var minX = Std.int(Math.max(tileX0, Math.floor(Math.min(v0x, Math.min(v1x, v2x)))));
|
||||
var maxX = Std.int(Math.min(tileX1, Math.ceil(Math.max(v0x, Math.max(v1x, v2x)))));
|
||||
var minY = Std.int(Math.max(tileY0, Math.floor(Math.min(v0y, Math.min(v1y, v2y)))));
|
||||
var maxY = Std.int(Math.min(tileY1, Math.ceil(Math.max(v0y, Math.max(v1y, v2y)))));
|
||||
if (minX > maxX || minY > maxY) return;
|
||||
|
||||
var d = (v1x - v0x) * (v2y - v0y) - (v2x - v0x) * (v1y - v0y);
|
||||
var invD = 1.0 / d;
|
||||
var dzdx = ((v1z - v0z) * (v2y - v0y) - (v2z - v0z) * (v1y - v0y)) * invD;
|
||||
var dzdy = ((v2z - v0z) * (v1x - v0x) - (v1z - v0z) * (v2x - v0x)) * invD;
|
||||
|
||||
var zPush = (Math.abs(dzdx) + Math.abs(dzdy)) * 0.5;
|
||||
var zMaxTri = v0z;
|
||||
if (v1z > zMaxTri) zMaxTri = v1z;
|
||||
if (v2z > zMaxTri) zMaxTri = v2z;
|
||||
|
||||
var cx0 = minX + 0.5;
|
||||
var cy0 = minY + 0.5;
|
||||
var e0row = e0a * cx0 + e0b * cy0 + e0c;
|
||||
var e1row = e1a * cx0 + e1b * cy0 + e1c;
|
||||
var e2row = e2a * cx0 + e2b * cy0 + e2c;
|
||||
var zrow = v0z + dzdx * (cx0 - v0x) + dzdy * (cy0 - v0y);
|
||||
|
||||
for (py in minY...maxY + 1) {
|
||||
var e0 = e0row; var e1 = e1row; var e2 = e2row;
|
||||
var z = zrow;
|
||||
var idx = py * w + minX;
|
||||
for (px in minX...maxX + 1) {
|
||||
if (e0 >= 0 && e1 >= 0 && e2 >= 0) {
|
||||
var zPix = z + zPush;
|
||||
if (zPix > zMaxTri) zPix = zMaxTri;
|
||||
if (zPix > 1.0) zPix = 1.0;
|
||||
if (zPix < buf[idx]) { buf[idx] = zPix; }
|
||||
}
|
||||
e0 += e0a; e1 += e1a; e2 += e2a;
|
||||
z += dzdx;
|
||||
idx++;
|
||||
}
|
||||
e0row += e0b; e1row += e1b; e2row += e2b;
|
||||
zrow += dzdy;
|
||||
}
|
||||
}
|
||||
|
||||
public function isOccluded(
|
||||
minX: Float, minY: Float, minZ: Float,
|
||||
maxX: Float, maxY: Float, maxZ: Float,
|
||||
vp: iron.math.Mat4,
|
||||
screenW: Int, screenH: Int
|
||||
): Bool {
|
||||
return hiz.isOccluded(minX, minY, minZ, maxX, maxY, maxZ, vp, screenW, screenH);
|
||||
}
|
||||
}
|
||||
79
leenkx/Sources/iron/culling/TemporalState.hx
Normal file
79
leenkx/Sources/iron/culling/TemporalState.hx
Normal file
@ -0,0 +1,79 @@
|
||||
package iron.culling;
|
||||
|
||||
class TemporalState {
|
||||
|
||||
public var visibleLastFrame: haxe.ds.IntMap<Bool>;
|
||||
public var visibleLastShadow: haxe.ds.IntMap<Bool>;
|
||||
public var frameLastTested: haxe.ds.IntMap<Int>;
|
||||
public var occlusionConfidence: haxe.ds.IntMap<Int>;
|
||||
public var capacity: Int = 0;
|
||||
public var frameCount: Int = 0;
|
||||
|
||||
public function new() {
|
||||
visibleLastFrame = new haxe.ds.IntMap<Bool>();
|
||||
visibleLastShadow = new haxe.ds.IntMap<Bool>();
|
||||
frameLastTested = new haxe.ds.IntMap<Int>();
|
||||
occlusionConfidence = new haxe.ds.IntMap<Int>();
|
||||
}
|
||||
|
||||
public function ensureCapacity(cap: Int) {
|
||||
if (capacity < cap) capacity = cap;
|
||||
}
|
||||
|
||||
public inline function wasVisibleLastFrame(i: Int): Bool {
|
||||
return visibleLastFrame.exists(i);
|
||||
}
|
||||
|
||||
public inline function wasVisibleShadowLastFrame(i: Int): Bool {
|
||||
return visibleLastShadow.exists(i);
|
||||
}
|
||||
|
||||
public inline function needsTest(i: Int, maxSkipFrames: Int): Bool {
|
||||
if (visibleLastFrame.exists(i)) return false; // assumed visible
|
||||
var lastTested = frameLastTested.get(i);
|
||||
if (lastTested == null) return true;
|
||||
return frameCount - lastTested >= maxSkipFrames;
|
||||
}
|
||||
|
||||
public inline function needsVisibleReTest(i: Int, retestInterval: Int): Bool {
|
||||
var lastTested = frameLastTested.get(i);
|
||||
if (lastTested == null) return true;
|
||||
return frameCount - lastTested >= retestInterval;
|
||||
}
|
||||
|
||||
public inline function setVisible(i: Int, visible: Bool) {
|
||||
if (visible) {
|
||||
visibleLastFrame.set(i, true);
|
||||
occlusionConfidence.remove(i);
|
||||
} else {
|
||||
visibleLastFrame.remove(i);
|
||||
var conf = occlusionConfidence.get(i);
|
||||
occlusionConfidence.set(i, conf == null ? 1 : Std.int(Math.min(255, conf + 1)));
|
||||
}
|
||||
}
|
||||
|
||||
public inline function markTested(i: Int) {
|
||||
frameLastTested.set(i, frameCount);
|
||||
}
|
||||
|
||||
public inline function setVisibleShadow(i: Int, visible: Bool) {
|
||||
if (visible) visibleLastShadow.set(i, true);
|
||||
else visibleLastShadow.remove(i);
|
||||
}
|
||||
|
||||
public inline function advanceFrame() {
|
||||
frameCount++;
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
visibleLastFrame = new haxe.ds.IntMap<Bool>();
|
||||
visibleLastShadow = new haxe.ds.IntMap<Bool>();
|
||||
frameLastTested = new haxe.ds.IntMap<Int>();
|
||||
occlusionConfidence = new haxe.ds.IntMap<Int>();
|
||||
}
|
||||
|
||||
public inline function getConfidence(i: Int): Int {
|
||||
var c = occlusionConfidence.get(i);
|
||||
return c == null ? 0 : c;
|
||||
}
|
||||
}
|
||||
@ -14,6 +14,7 @@ import iron.math.Vec4;
|
||||
import iron.math.Mat4;
|
||||
import iron.data.SceneFormat;
|
||||
import iron.data.MeshData;
|
||||
import iron.data.ShaderData;
|
||||
|
||||
class Geometry {
|
||||
#if lnx_deinterleaved
|
||||
@ -57,6 +58,7 @@ class Geometry {
|
||||
// Skinned
|
||||
#if lnx_skin
|
||||
public var skeletonTransformsI: Array<Mat4> = null;
|
||||
public var skeletonBoneRefs: Array<String> = null;
|
||||
public var actions: Map<String, Array<iron.data.SceneFormat.TObj>> = new Map();
|
||||
public var mats: Map<String, Array<Mat4>> = new Map();
|
||||
#end
|
||||
@ -208,7 +210,7 @@ class Geometry {
|
||||
for (v in 0...vertexBuffers.length)
|
||||
if (vertexBuffers[v].name == e.name) {
|
||||
vbs.push(vertexBuffers[v].buffer);
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (e.name == "ipos" && instancedVB != null) {
|
||||
vbs.push(instancedVB);
|
||||
@ -264,7 +266,7 @@ class Geometry {
|
||||
for (i in 0...vaLength)
|
||||
vertexBuffers[i] = {
|
||||
name: vertexArrays[i].attrib,
|
||||
buffer: makeDeinterleavedVB(vertexArrays[i].values, vertexArrays[i].attrib, vertexArrays[i].size)
|
||||
buffer: makeDeinterleavedVB(vertexArrays[i])
|
||||
};
|
||||
#else
|
||||
|
||||
@ -295,14 +297,20 @@ class Geometry {
|
||||
}
|
||||
|
||||
#if lnx_deinterleaved
|
||||
function makeDeinterleavedVB(data: ByteArray, name: String, structLength: Int): VertexBuffer {
|
||||
function makeDeinterleavedVB(va: TVertexArray): VertexBuffer {
|
||||
var struct = new VertexStructure();
|
||||
struct.add(name, structLength == 2 ? VertexData.Short2Norm : VertexData.Short4Norm);
|
||||
struct.add(va.attrib, ShaderContext.parseData(va.data));
|
||||
|
||||
var vertexBuffer = new VertexBuffer(Std.int(data.byteLength / 2 / structLength), struct, usage);
|
||||
var vcount = verticesCount(va);
|
||||
var vertexBuffer = new VertexBuffer(vcount, struct, usage);
|
||||
|
||||
var stride = Std.int(struct.byteSize() / 2);
|
||||
var vertices = vertexBuffer.lock();
|
||||
for (i in 0...Std.int(vertices.byteLength / 2)) vertices.setInt16(i * 2, data.getInt16(i * 2));
|
||||
var di = -1;
|
||||
for (i in 0...vcount) {
|
||||
for (o in 0...va.size) vertices.setInt16(++di * 2, va.values[i * va.size + o]);
|
||||
for (o in va.size...stride) vertices.setInt16(++di * 2, 0);
|
||||
}
|
||||
vertexBuffer.unlock();
|
||||
|
||||
return vertexBuffer;
|
||||
@ -319,6 +327,33 @@ class Geometry {
|
||||
|
||||
// Skinned
|
||||
#if lnx_skin
|
||||
public function addArmature(armature: iron.data.Armature) {
|
||||
for (a in armature.actions) {
|
||||
addAction(a.bones, a.name);
|
||||
}
|
||||
}
|
||||
|
||||
public function addAction(bones: Array<TObj>, name: String) {
|
||||
if (bones == null || skeletonBoneRefs == null) return;
|
||||
if (actions.get(name) != null) return;
|
||||
var actionBones: Array<TObj> = [];
|
||||
|
||||
for (s in skeletonBoneRefs) {
|
||||
for (b in bones) {
|
||||
if (b.name == s) {
|
||||
actionBones.push(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
actions.set(name, actionBones);
|
||||
|
||||
var actionMats: Array<Mat4> = [];
|
||||
for (b in actionBones) {
|
||||
actionMats.push(Mat4.fromFloat32Array(b.transform.values));
|
||||
}
|
||||
mats.set(name, actionMats);
|
||||
}
|
||||
|
||||
public function initSkeletonTransforms(transformsI: Array<Float32Array>) {
|
||||
skeletonTransformsI = [];
|
||||
for (t in transformsI) {
|
||||
|
||||
@ -57,10 +57,8 @@ class MeshBatch {
|
||||
}
|
||||
|
||||
public function removeMesh(m: MeshObject) {
|
||||
var shader = m.materials[0].shader;
|
||||
var b = buckets.get(shader);
|
||||
if (b != null) b.removeMesh(m);
|
||||
else nonBatched.remove(m);
|
||||
var b = m.materials.length > 0 ? buckets.get(m.materials[0].shader) : null;
|
||||
if (b == null || !b.removeMesh(m)) nonBatched.remove(m);
|
||||
}
|
||||
|
||||
@:access(iron.RenderPath)
|
||||
@ -71,30 +69,52 @@ class MeshBatch {
|
||||
if (b.meshes.length > 0 && b.meshes[0].cullMaterial(context)) continue;
|
||||
|
||||
var scontext = b.shader.getContext(context);
|
||||
if (scontext == null) continue;
|
||||
g.setPipeline(scontext.pipeState);
|
||||
// #if lnx_deinterleaved // TODO
|
||||
// g.setVertexBuffers(b.vertexBuffers);
|
||||
// #else
|
||||
#if lnx_deinterleaved
|
||||
g.setVertexBuffers(b.getVertexBuffers(scontext.raw.vertex_elements));
|
||||
#else
|
||||
g.setVertexBuffer(b.getVertexBuffer(scontext.raw.vertex_elements));
|
||||
// #end
|
||||
#end
|
||||
g.setIndexBuffer(b.indexBuffer);
|
||||
|
||||
Uniforms.setContextConstants(g, scontext, bindParams);
|
||||
|
||||
RenderPath.sortMeshesDistance(b.meshes);
|
||||
var isShadowCtx = context == "shadowmap" || context == "shadowmap_transparent";
|
||||
if (!isShadowCtx) RenderPath.sortMeshesDistance(b.meshes);
|
||||
|
||||
for (m in b.meshes) {
|
||||
if (!m.visible) continue; // Skip render if object is hidden
|
||||
#if lnx_occlusion_culling
|
||||
if (!m.occlusionVisible && (context == "mesh" || context == "depth" || context == "translucent")) continue;
|
||||
if (!m.shadowOcclusionVisible && isShadowCtx) continue;
|
||||
#end
|
||||
if (m.cullMesh(context, Scene.active.camera, RenderPath.active.light)) continue;
|
||||
|
||||
// Get context
|
||||
var materialContexts: Array<MaterialContext> = [];
|
||||
var shaderContexts: Array<ShaderContext> = [];
|
||||
m.getContexts(context, m.materials, materialContexts, shaderContexts);
|
||||
var materialContexts: Array<MaterialContext> = null;
|
||||
var shaderContexts: Array<ShaderContext> = null;
|
||||
if (m.cachedMaterialContexts != null && MeshObject.matsEqual(m.cachedMats.get(context), m.materials)) {
|
||||
materialContexts = m.cachedMaterialContexts.get(context);
|
||||
shaderContexts = m.cachedShaderContexts.get(context);
|
||||
}
|
||||
if (materialContexts == null) {
|
||||
materialContexts = [];
|
||||
shaderContexts = [];
|
||||
m.getContexts(context, m.materials, materialContexts, shaderContexts);
|
||||
if (m.cachedMaterialContexts == null) {
|
||||
m.cachedMats = new Map();
|
||||
m.cachedMaterialContexts = new Map();
|
||||
m.cachedShaderContexts = new Map();
|
||||
}
|
||||
m.cachedMats.set(context, m.materials);
|
||||
m.cachedMaterialContexts.set(context, materialContexts);
|
||||
m.cachedShaderContexts.set(context, shaderContexts);
|
||||
}
|
||||
|
||||
Uniforms.posUnpack = m.data.scalePos;
|
||||
Uniforms.texUnpack = m.data.scaleTex;
|
||||
m.transform.update();
|
||||
if (!isShadowCtx) m.transform.update();
|
||||
|
||||
// Render mesh
|
||||
Uniforms.setObjectConstants(g, scontext, m);
|
||||
@ -126,8 +146,12 @@ class Bucket {
|
||||
|
||||
public var batched = false;
|
||||
public var shader: ShaderData;
|
||||
#if lnx_deinterleaved
|
||||
public var vertexBuffers: Array<InterleavedVertexBuffer> = [];
|
||||
#else
|
||||
var vertexBuffer: VertexBuffer;
|
||||
var vertexBufferMap: Map<String, VertexBuffer> = new Map();
|
||||
#end
|
||||
public var indexBuffer: IndexBuffer;
|
||||
public var meshes: Array<MeshObject> = [];
|
||||
|
||||
@ -137,8 +161,12 @@ class Bucket {
|
||||
|
||||
public function remove() {
|
||||
indexBuffer.delete();
|
||||
#if lnx_deinterleaved
|
||||
for (buf in vertexBuffers) buf.buffer.delete();
|
||||
#else
|
||||
// this.vertexBuffer is in the map, so it's also deleted here
|
||||
for (buf in vertexBufferMap) buf.delete();
|
||||
#end
|
||||
meshes = [];
|
||||
}
|
||||
|
||||
@ -146,10 +174,24 @@ class Bucket {
|
||||
meshes.push(m);
|
||||
}
|
||||
|
||||
public function removeMesh(m: MeshObject) {
|
||||
meshes.remove(m);
|
||||
public function removeMesh(m: MeshObject): Bool {
|
||||
return meshes.remove(m);
|
||||
}
|
||||
|
||||
#if lnx_deinterleaved
|
||||
public function getVertexBuffers(elems: Array<TVertexElement>): Array<VertexBuffer> {
|
||||
var vbs: Array<VertexBuffer> = [];
|
||||
for (e in elems) {
|
||||
for (v in vertexBuffers) {
|
||||
if (v.name == e.name) {
|
||||
vbs.push(v.buffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return vbs;
|
||||
}
|
||||
#else
|
||||
function copyAttribute(attribSize: Int, count: Int, to: ByteArray, toStride: Int, toOffset: Int, from: ByteArray, fromStride: Int, fromOffset: Int) {
|
||||
for (i in 0...count) {
|
||||
for (j in 0...attribSize) {
|
||||
@ -195,6 +237,7 @@ class Bucket {
|
||||
}
|
||||
return vb;
|
||||
}
|
||||
#end
|
||||
|
||||
function vertexCount(g: Geometry, hasUVs: Bool): Int {
|
||||
var vcount = g.getVerticesLength();
|
||||
@ -239,6 +282,50 @@ class Bucket {
|
||||
|
||||
if (mdatas.length == 0) return;
|
||||
|
||||
#if lnx_deinterleaved
|
||||
var attrNames: Array<String> = [];
|
||||
var attrReps = new Map<String, TVertexArray>();
|
||||
for (md in mdatas) {
|
||||
for (va in md.geom.vertexArrays) {
|
||||
if (attrReps.get(va.attrib) == null) {
|
||||
attrReps.set(va.attrib, va);
|
||||
attrNames.push(va.attrib);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (name in attrNames) {
|
||||
var rep = attrReps.get(name);
|
||||
var attrStruct = new VertexStructure();
|
||||
attrStruct.add(name, ShaderContext.parseData(rep.data));
|
||||
var stride = Std.int(attrStruct.byteSize() / 2);
|
||||
var vtotal = 0;
|
||||
for (md in mdatas) vtotal += md.geom.getVerticesCount();
|
||||
var vb = new VertexBuffer(vtotal, attrStruct, Usage.StaticUsage);
|
||||
var vertices = vb.lock();
|
||||
var ai = -1;
|
||||
for (md in mdatas) {
|
||||
var va: TVertexArray = null;
|
||||
for (v in md.geom.vertexArrays) {
|
||||
if (v.attrib == name) {
|
||||
va = v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (va != null && va.values != null) {
|
||||
var nverts = md.geom.getVerticesCount();
|
||||
for (i in 0...nverts) {
|
||||
for (o in 0...va.size) vertices.setInt16(++ai * 2, va.values[i * va.size + o]);
|
||||
for (o in va.size...stride) vertices.setInt16(++ai * 2, 0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i in 0...md.geom.getVerticesCount() * stride) vertices.setInt16(++ai * 2, 0);
|
||||
}
|
||||
}
|
||||
vb.unlock();
|
||||
vertexBuffers.push({name: name, buffer: vb});
|
||||
}
|
||||
#else
|
||||
// Pick UVs if present
|
||||
var vs = mdatas[0].geom.struct;
|
||||
for (md in mdatas) if (md.geom.struct.size() > vs.size()) vs = md.geom.struct;
|
||||
@ -256,6 +343,7 @@ class Bucket {
|
||||
var s = "";
|
||||
for (e in vs.elements) s += e.name;
|
||||
vertexBufferMap.set(s, vertexBuffer);
|
||||
#end
|
||||
|
||||
indexBuffer = new IndexBuffer(icount, Usage.StaticUsage);
|
||||
var indices = indexBuffer.lock();
|
||||
|
||||
@ -108,6 +108,7 @@ class MeshData {
|
||||
// Skinned
|
||||
#if lnx_skin
|
||||
if (raw.skin != null) {
|
||||
dat.geom.skeletonBoneRefs = raw.skin.bone_ref_array;
|
||||
dat.geom.initSkeletonTransforms(raw.skin.transformsI);
|
||||
}
|
||||
#end
|
||||
|
||||
@ -60,6 +60,16 @@ typedef TMeshData = {
|
||||
@:optional public var scale_pos: Null<FastFloat>; // Unpack pos from (-1,1) coords
|
||||
@:optional public var scale_tex: Null<FastFloat>; // Unpack tex from (-1,1) coords
|
||||
@:optional public var morph_target: TMorphTarget;
|
||||
@:optional public var occluder_proxy: TOccluderProxy;
|
||||
}
|
||||
|
||||
#if js
|
||||
typedef TOccluderProxy = {
|
||||
#else
|
||||
@:structInit class TOccluderProxy {
|
||||
#end
|
||||
public var positions: Float32Array;
|
||||
public var indices: Uint32Array;
|
||||
}
|
||||
|
||||
#if js
|
||||
|
||||
@ -55,6 +55,7 @@ class ShaderData {
|
||||
if (raw == null) {
|
||||
trace('Shader data "$name" not found!');
|
||||
done(null);
|
||||
return;
|
||||
}
|
||||
new ShaderData(raw, done, overrideContext);
|
||||
});
|
||||
@ -101,8 +102,9 @@ class ShaderContext {
|
||||
constants = [];
|
||||
textureUnits = [];
|
||||
|
||||
var instStruct: VertexStructure = null;
|
||||
if (instancingType > 0) { // Instancing
|
||||
var instStruct = new VertexStructure();
|
||||
instStruct = new VertexStructure();
|
||||
instStruct.add("ipos", VertexData.Float3);
|
||||
if (instancingType == 2 || instancingType == 4) {
|
||||
instStruct.add("irot", VertexData.Float3);
|
||||
@ -116,11 +118,19 @@ class ShaderContext {
|
||||
this.instanceStride = Std.int(instStruct.byteSize() / 4);
|
||||
|
||||
instStruct.instanced = true;
|
||||
pipeState.inputLayout = [structure, instStruct];
|
||||
}
|
||||
else { // Regular
|
||||
pipeState.inputLayout = [structure];
|
||||
#if lnx_deinterleaved
|
||||
var layouts: Array<VertexStructure> = [];
|
||||
for (e in structure.elements) {
|
||||
var s = new VertexStructure();
|
||||
s.add(e.name, e.data);
|
||||
layouts.push(s);
|
||||
}
|
||||
#else
|
||||
var layouts = [structure];
|
||||
#end
|
||||
if (instStruct != null) layouts.push(instStruct);
|
||||
pipeState.inputLayout = layouts;
|
||||
|
||||
// Depth
|
||||
pipeState.depthWrite = raw.depth_write;
|
||||
|
||||
@ -49,7 +49,7 @@ class Reader {
|
||||
* @see https://msdn.microsoft.com/en-us/library/windows/desktop/dd318229(v=vs.85).aspx
|
||||
* @see https://en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header
|
||||
*/
|
||||
public function read() : format.bmp.Data {
|
||||
public function read() : iron.format.bmp.Data {
|
||||
// Read Header
|
||||
for (b in ["B".code, "M".code]) {
|
||||
if (input.readByte() != b) throw "Invalid header";
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
package format.gif;
|
||||
import format.gif.Data;
|
||||
package iron.format.gif;
|
||||
import iron.format.gif.Data;
|
||||
import haxe.ds.Vector;
|
||||
import haxe.io.Bytes;
|
||||
import haxe.io.Output;
|
||||
|
||||
@ -29,7 +29,7 @@
|
||||
*/
|
||||
package iron.format.jpg;
|
||||
|
||||
class Writer {
|
||||
class WriterOriginal {
|
||||
var ZigZag: Array<Int>;
|
||||
|
||||
// Static table initialization
|
||||
|
||||
@ -40,11 +40,6 @@ class Animation {
|
||||
public var paused = false;
|
||||
var frameTime: FastFloat = 1 / 60;
|
||||
|
||||
var blendTime: FastFloat = 0.0;
|
||||
var blendCurrent: FastFloat = 0.0;
|
||||
var blendFactor: FastFloat = 0.0;
|
||||
|
||||
var lastFrameIndex = -1;
|
||||
var markerEvents: Map<ActionSampler, Map<String, Array<Void->Void>>> = null;
|
||||
|
||||
public var activeActions: Map<String, ActionSampler> = null;
|
||||
@ -63,8 +58,6 @@ class Animation {
|
||||
|
||||
public function play(action = "", onComplete: Void->Void = null, blendTime = 0.0, speed = 1.0, loop = true) {
|
||||
if (blendTime > 0) {
|
||||
this.blendTime = blendTime;
|
||||
this.blendCurrent = 0.0;
|
||||
frameIndex = 0;
|
||||
time = 0.0;
|
||||
}
|
||||
@ -93,6 +86,7 @@ class Animation {
|
||||
}
|
||||
|
||||
public function update(delta: FastFloat) {
|
||||
if (paused) return;
|
||||
if(activeActions == null) return;
|
||||
|
||||
for(sampler in activeActions){
|
||||
@ -160,24 +154,25 @@ class Animation {
|
||||
while (checkFrameIndex(track.frames, time, frameIndex, speed)) frameIndex += sign;
|
||||
|
||||
// Marker events
|
||||
if (markerEvents != null && anim.marker_names != null && frameIndex != lastFrameIndex) {
|
||||
if(markerEvents.get(sampler) != null){
|
||||
if (markerEvents != null && anim.marker_names != null && frameIndex != sampler.offsetOld) {
|
||||
var markerListeners = markerEvents.get(sampler);
|
||||
if (markerListeners != null) {
|
||||
var fromFrame = sampler.offsetOld < 0 ?
|
||||
(speed > 0 ? -1 : track.frames[track.frames.length - 1] + 1) :
|
||||
track.frames[sampler.offsetOld];
|
||||
var toFrame = track.frames[frameIndex];
|
||||
for (i in 0...anim.marker_frames.length) {
|
||||
if (frameIndex == anim.marker_frames[i]) {
|
||||
var markerAct = markerEvents.get(sampler);
|
||||
var ar = markerAct.get(anim.marker_names[i]);
|
||||
if (ar != null) for (f in ar) f();
|
||||
} else {
|
||||
for (j in 0...(frameIndex - lastFrameIndex)) {
|
||||
if (lastFrameIndex + j + 1 == anim.marker_frames[i]) {
|
||||
var markerAct = markerEvents.get(sampler);
|
||||
var ar = markerAct.get(anim.marker_names[i]);
|
||||
if (ar != null) for (f in ar) f();
|
||||
}
|
||||
}
|
||||
var markerFrame = anim.marker_frames[i];
|
||||
var crossed =
|
||||
(speed > 0 && toFrame >= fromFrame && markerFrame > fromFrame && markerFrame <= toFrame) ||
|
||||
(speed < 0 && toFrame <= fromFrame && markerFrame <= fromFrame && markerFrame > toFrame) ||
|
||||
(speed > 0 && toFrame < fromFrame && (markerFrame > fromFrame || markerFrame <= toFrame)) ||
|
||||
(speed < 0 && toFrame > fromFrame && (markerFrame >= toFrame || markerFrame < fromFrame));
|
||||
if (crossed) {
|
||||
var listeners = markerListeners.get(anim.marker_names[i]);
|
||||
if (listeners != null) for (listener in listeners) listener();
|
||||
}
|
||||
}
|
||||
lastFrameIndex = frameIndex;
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,8 +186,8 @@ class Animation {
|
||||
else {
|
||||
frameIndex -= sign;
|
||||
sampler.paused = true;
|
||||
if (sampler.onComplete != null) for(func in sampler.onComplete){ func();};
|
||||
}
|
||||
if (sampler.onComplete != null) for(func in sampler.onComplete){ func();};
|
||||
sampler.trackEnd = true;
|
||||
}
|
||||
|
||||
|
||||
@ -112,6 +112,10 @@ class BoneAnimation extends Animation {
|
||||
this.data = mo != null ? mo.data : null;
|
||||
this.isSkinned = data != null ? data.isSkinned : false;
|
||||
if (this.isSkinned) {
|
||||
if (armature != null && data.geom.skeletonBoneRefs != null
|
||||
&& data.geom.actions.keys().hasNext() == false) {
|
||||
data.geom.addArmature(armature);
|
||||
}
|
||||
var boneSize = 12; // Dual-quat skinning + scaling
|
||||
this.skinBuffer = new Float32Array(skinMaxBones * boneSize);
|
||||
for (i in 0...this.skinBuffer.length) this.skinBuffer[i] = 0;
|
||||
@ -205,18 +209,37 @@ class BoneAnimation extends Animation {
|
||||
}
|
||||
|
||||
function setAction(action: String) {
|
||||
if (armature == null) return;
|
||||
armature.initMats();
|
||||
var a = armature.getAction(action);
|
||||
if (a == null) return;
|
||||
skeletonBones = a.bones;
|
||||
skeletonMats = a.mats;
|
||||
if(! rootMotionCacheInit) skeletonMats.push(Mat4.identity());
|
||||
if (isSkinned) {
|
||||
skeletonBones = data.geom.actions.get(action);
|
||||
var geomMats = data.geom.mats.get(action);
|
||||
if (geomMats != null) {
|
||||
skeletonMats = [];
|
||||
for (i in 0...geomMats.length) {
|
||||
var m = Mat4.identity();
|
||||
m.setFrom(geomMats[i]);
|
||||
skeletonMats.push(m);
|
||||
}
|
||||
}
|
||||
else skeletonMats = null;
|
||||
}
|
||||
if (skeletonBones == null) {
|
||||
if (armature == null) return;
|
||||
armature.initMats();
|
||||
var a = armature.getAction(action);
|
||||
if (a == null) return;
|
||||
skeletonBones = a.bones;
|
||||
skeletonMats = a.mats;
|
||||
}
|
||||
if (skeletonMats.length == skeletonBones.length) skeletonMats.push(Mat4.identity()); // Root motion cache
|
||||
rootMotionCacheInit = true;
|
||||
setMats();
|
||||
}
|
||||
|
||||
function getAction(action: String): Array<TObj> {
|
||||
if (isSkinned) {
|
||||
var bones = data.geom.actions.get(action);
|
||||
if (bones != null) return bones;
|
||||
}
|
||||
if (armature == null) return null;
|
||||
armature.initMats();
|
||||
var a = armature.getAction(action);
|
||||
@ -251,15 +274,91 @@ class BoneAnimation extends Animation {
|
||||
updateAnimation = f;
|
||||
}
|
||||
|
||||
var blendSampler1: ActionSampler = null;
|
||||
var blendSampler2: ActionSampler = null;
|
||||
var blendMats: Array<Mat4> = null;
|
||||
|
||||
public function blend(action1: String, action2: String, factor: FastFloat) {
|
||||
var actionName1: String = getName(action1);
|
||||
var actionName2: String = getName(action2);
|
||||
if (blendSampler1 == null || blendSampler1.action != actionName1) {
|
||||
blendSampler1 = new ActionSampler(actionName1);
|
||||
registerAction("blendSampler1", blendSampler1);
|
||||
}
|
||||
if (blendSampler2 == null || blendSampler2.action != actionName2) {
|
||||
blendSampler2 = new ActionSampler(actionName2);
|
||||
registerAction("blendSampler2", blendSampler2);
|
||||
}
|
||||
blendSampler1.paused = false;
|
||||
blendSampler2.paused = false;
|
||||
if (transitionSampler != null) {
|
||||
transitionSampler.paused = true;
|
||||
transitionSampler = null;
|
||||
}
|
||||
if (currentPlaySampler != null) currentPlaySampler.paused = true;
|
||||
currentPlaySampler = blendSampler1;
|
||||
if (skeletonMats == null) setAction(actionName1);
|
||||
if (skeletonMats == null) return;
|
||||
if (blendMats == null) blendMats = initMatsEmpty();
|
||||
updateAnimation = function(mats) {
|
||||
sampleAction(blendSampler1, mats);
|
||||
sampleAction(blendSampler2, blendMats);
|
||||
blendAction(mats, blendMats, mats, factor);
|
||||
}
|
||||
}
|
||||
|
||||
var playSamplers: Map<String, ActionSampler> = [];
|
||||
var currentPlaySampler: ActionSampler = null;
|
||||
var transitionSampler: ActionSampler = null;
|
||||
var transitionMats: Array<Mat4> = null;
|
||||
var transitionTime: FastFloat = 0.0;
|
||||
var transitionDuration: FastFloat = 0.0;
|
||||
|
||||
override public function play(action = "", onComplete: Void->Void = null, blendTime = 0.2, speed = 1.0, loop = true) {
|
||||
var actionName: String = getName(action);
|
||||
if (actionName != "") {
|
||||
setAction(actionName);
|
||||
super.play(actionName, onComplete, blendTime, speed, loop);
|
||||
var tempAnimParam = new ActionSampler(actionName);
|
||||
registerAction("tempAction", tempAnimParam);
|
||||
var sampler = playSamplers.get(actionName);
|
||||
if (sampler == null) {
|
||||
if (activeActions != null) sampler = activeActions.get(actionName);
|
||||
if (sampler == null) sampler = new ActionSampler(actionName);
|
||||
playSamplers.set(actionName, sampler);
|
||||
}
|
||||
if (transitionSampler != null) {
|
||||
transitionSampler.paused = true;
|
||||
transitionSampler = null;
|
||||
}
|
||||
if (blendTime > 0 && currentPlaySampler != null && currentPlaySampler != sampler && skeletonMats != null) {
|
||||
transitionSampler = currentPlaySampler;
|
||||
if (transitionMats == null) transitionMats = initMatsEmpty();
|
||||
transitionTime = 0.0;
|
||||
transitionDuration = blendTime;
|
||||
if (blendSampler1 != null && blendSampler1 != transitionSampler) blendSampler1.paused = true;
|
||||
if (blendSampler2 != null && blendSampler2 != transitionSampler) blendSampler2.paused = true;
|
||||
}
|
||||
else if (currentPlaySampler != null) currentPlaySampler.paused = true;
|
||||
currentPlaySampler = sampler;
|
||||
sampler.speed = speed;
|
||||
sampler.loop = loop;
|
||||
sampler.onComplete = onComplete != null ? [onComplete] : null;
|
||||
sampler.restartAction();
|
||||
registerAction(actionName, sampler);
|
||||
updateAnimation = function(mats){
|
||||
sampleAction(tempAnimParam, mats);
|
||||
if (transitionSampler != null) {
|
||||
sampleAction(transitionSampler, transitionMats);
|
||||
sampleAction(sampler, mats);
|
||||
var f = transitionTime / transitionDuration;
|
||||
if (f > 1.0) f = 1.0;
|
||||
f = f * f * (3.0 - 2.0 * f); // Smoothstep
|
||||
blendAction(transitionMats, mats, mats, f, -1, 0.0);
|
||||
transitionTime += delta;
|
||||
if (transitionTime >= transitionDuration) {
|
||||
transitionSampler.paused = true;
|
||||
transitionSampler = null;
|
||||
}
|
||||
}
|
||||
else sampleAction(sampler, mats);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -279,6 +378,7 @@ class BoneAnimation extends Animation {
|
||||
#end
|
||||
|
||||
super.update(delta);
|
||||
if (paused) return;
|
||||
if(updateAnimation != null) {
|
||||
updateAnimation(skeletonMats);
|
||||
}
|
||||
@ -540,12 +640,14 @@ class BoneAnimation extends Animation {
|
||||
|
||||
|
||||
inline function interpolateSample(track: TTrack, m: Mat4, t: FastFloat, ti: Int, sign: Int) {
|
||||
var tj = ti + sign;
|
||||
if (tj < 0 || tj >= track.frames.length) tj = ti;
|
||||
var t1 = track.frames[ti] * frameTime;
|
||||
var t2 = track.frames[ti + sign] * frameTime;
|
||||
var s: FastFloat = (t - t1) / (t2 - t1); // Linear
|
||||
var t2 = track.frames[tj] * frameTime;
|
||||
var s: FastFloat = t2 != t1 ? (t - t1) / (t2 - t1) : 0.0; // Linear
|
||||
|
||||
m1.setF32(track.values, ti * 16); // Offset to 4x4 matrix array
|
||||
m2.setF32(track.values, (ti + sign) * 16);
|
||||
m2.setF32(track.values, tj * 16);
|
||||
|
||||
// Decompose
|
||||
m1.decompose(vpos, q1, vscl);
|
||||
@ -643,7 +745,7 @@ class BoneAnimation extends Animation {
|
||||
return actionMats != null ? actionMats[getBoneIndex(bone)] : null;
|
||||
}
|
||||
|
||||
function getWorldMat(actionMats: Array<Mat4>, bone: TObj): Mat4 {
|
||||
public function getWorldMat(actionMats: Array<Mat4>, bone: TObj): Mat4 {
|
||||
if (applyParent == null) {
|
||||
applyParent = [];
|
||||
for (m in actionMats) applyParent.push(true);
|
||||
@ -728,7 +830,7 @@ class BoneAnimation extends Animation {
|
||||
// Skip if root motion is disabled
|
||||
if(j < 0) continue;
|
||||
// Use Action 2
|
||||
if(skeletonBones[j].bone_layers[layerMask] || layerMask < 0){
|
||||
if(layerMask < 0 || skeletonBones[j].bone_layers[layerMask]){
|
||||
resultMat[i].setFrom(actionMats2[i]);
|
||||
}
|
||||
// Use Action 1 if not in layer
|
||||
@ -744,7 +846,7 @@ class BoneAnimation extends Animation {
|
||||
// Skip if root motion is disabled
|
||||
if(j < 0) continue;
|
||||
// Blend
|
||||
if(skeletonBones[j].bone_layers[j] || layerMask < 0) {
|
||||
if(layerMask < 0 || skeletonBones[j].bone_layers[layerMask]) {
|
||||
// Decompose
|
||||
m.setFrom(actionMats1[i]);
|
||||
m1.setFrom(actionMats2[i]);
|
||||
@ -786,8 +888,10 @@ class BoneAnimation extends Animation {
|
||||
}
|
||||
else{
|
||||
for(i in 0...baseActionMats.length){
|
||||
var j = i == skeletonBones.length ? rootMotionIndex : i;
|
||||
if(j < 0) continue;
|
||||
|
||||
if(skeletonBones[i].bone_layers[layerMask] || layerMask < 0) {
|
||||
if(layerMask < 0 || skeletonBones[j].bone_layers[layerMask]) {
|
||||
// Decompose
|
||||
m.setFrom(baseActionMats[i]);
|
||||
m1.setFrom(addActionMats[i]);
|
||||
|
||||
@ -62,7 +62,20 @@ class CameraObject extends Object {
|
||||
|
||||
public function buildProjection(screenAspect: Null<Float> = null) {
|
||||
if (data.raw.ortho != null) {
|
||||
P = Mat4.ortho(data.raw.ortho[0], data.raw.ortho[1], data.raw.ortho[2], data.raw.ortho[3], data.raw.near_plane, data.raw.far_plane);
|
||||
var left = data.raw.ortho[0];
|
||||
var right = data.raw.ortho[1];
|
||||
var bottom = data.raw.ortho[2];
|
||||
var top = data.raw.ortho[3];
|
||||
if (data.raw.aspect != null) {
|
||||
var aspect: kha.FastFloat = data.raw.aspect;
|
||||
if (screenAspect == null) screenAspect = iron.App.w() / iron.App.h();
|
||||
var scale = aspect / screenAspect;
|
||||
var cx = (left + right) * 0.5;
|
||||
var hw = (right - left) * 0.5 * scale;
|
||||
left = cx - hw;
|
||||
right = cx + hw;
|
||||
}
|
||||
P = Mat4.ortho(left, right, bottom, top, data.raw.near_plane, data.raw.far_plane);
|
||||
}
|
||||
else {
|
||||
if (screenAspect == null) screenAspect = iron.App.w() / iron.App.h();
|
||||
@ -247,25 +260,3 @@ class CameraObject extends Object {
|
||||
return new Vec4(-transform.world._20, -transform.world._21, -transform.world._22);
|
||||
}
|
||||
}
|
||||
|
||||
class FrustumPlane {
|
||||
public var normal = new Vec4(1.0, 0.0, 0.0);
|
||||
public var constant = 0.0;
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function normalize() {
|
||||
var inverseNormalLength = 1.0 / normal.length();
|
||||
normal.mult(inverseNormalLength);
|
||||
constant *= inverseNormalLength;
|
||||
}
|
||||
|
||||
public function distanceToSphere(sphereCenter: Vec4, sphereRadius: Float): Float {
|
||||
return (normal.dot(sphereCenter) + constant) - sphereRadius;
|
||||
}
|
||||
|
||||
public inline function setComponents(x: Float, y: Float, z: Float, w: Float) {
|
||||
normal.set(x, y, z);
|
||||
constant = w;
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,10 @@ class Constraint {
|
||||
}
|
||||
|
||||
public function apply(transform: Transform) {
|
||||
if (target == null && raw.target != null) target = Scene.active.getChild(raw.target).transform;
|
||||
if (target == null && raw.target != null) {
|
||||
var o = Scene.active.getChild(raw.target);
|
||||
if (o != null) target = o.transform;
|
||||
}
|
||||
|
||||
if (target == null && raw.type != "LIMIT_LOCATION" && raw.type != "LIMIT_ROTATION" && raw.type != "LIMIT_SCALE") return;
|
||||
|
||||
|
||||
25
leenkx/Sources/iron/object/FrustumPlane.hx
Normal file
25
leenkx/Sources/iron/object/FrustumPlane.hx
Normal file
@ -0,0 +1,25 @@
|
||||
package iron.object;
|
||||
|
||||
import iron.math.Vec4;
|
||||
|
||||
class FrustumPlane {
|
||||
public var normal = new Vec4(1.0, 0.0, 0.0);
|
||||
public var constant = 0.0;
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function normalize() {
|
||||
var inverseNormalLength = 1.0 / normal.length();
|
||||
normal.mult(inverseNormalLength);
|
||||
constant *= inverseNormalLength;
|
||||
}
|
||||
|
||||
public function distanceToSphere(sphereCenter: Vec4, sphereRadius: Float): Float {
|
||||
return (normal.dot(sphereCenter) + constant) - sphereRadius;
|
||||
}
|
||||
|
||||
public inline function setComponents(x: Float, y: Float, z: Float, w: Float) {
|
||||
normal.set(x, y, z);
|
||||
constant = w;
|
||||
}
|
||||
}
|
||||
@ -34,7 +34,7 @@ class LightObject extends Object {
|
||||
// Cascades
|
||||
public static var cascadeCount = 1;
|
||||
public static var cascadeSplitFactor = 0.8;
|
||||
public static var cascadeBounds = 1.0;
|
||||
public static var cascadeBounds = 2.0; // occlusion culling shadows pop with 1.0
|
||||
#if lnx_csm
|
||||
var cascadeData: Float32Array = null;
|
||||
var cascadeVP: Array<Mat4>;
|
||||
@ -140,6 +140,9 @@ class LightObject extends Object {
|
||||
}
|
||||
#end
|
||||
#end
|
||||
#if lnx_occlusion_culling
|
||||
if (iron.culling.CullSystem.active != null) iron.culling.CullSystem.active.removeLight(this);
|
||||
#end
|
||||
super.remove();
|
||||
}
|
||||
|
||||
@ -278,12 +281,13 @@ class LightObject extends Object {
|
||||
var worldPerTexelX = (maxx - minx) / smsize;
|
||||
var worldPerTexelY = (maxy - miny) / smsize;
|
||||
var worldPerTexelZ = (maxz - minz) / smsize;
|
||||
var ex = maxx - minx, ey = maxy - miny, ez = maxz - minz;
|
||||
minx = Math.floor(minx / worldPerTexelX) * worldPerTexelX;
|
||||
miny = Math.floor(miny / worldPerTexelY) * worldPerTexelY;
|
||||
minz = Math.floor(minz / worldPerTexelZ) * worldPerTexelZ;
|
||||
maxx = Math.floor(maxx / worldPerTexelX) * worldPerTexelX;
|
||||
maxy = Math.floor(maxy / worldPerTexelY) * worldPerTexelY;
|
||||
maxz = Math.floor(maxz / worldPerTexelZ) * worldPerTexelZ;
|
||||
maxx = minx + ex;
|
||||
maxy = miny + ey;
|
||||
maxz = minz + ez;
|
||||
|
||||
var hx = (maxx - minx) / 2;
|
||||
var hy = (maxy - miny) / 2;
|
||||
|
||||
@ -30,13 +30,24 @@ class MeshObject extends Object {
|
||||
public var frustumCulling = true;
|
||||
public var activeTilesheet: Tilesheet = null;
|
||||
public var tilesheets: Array<Tilesheet> = null;
|
||||
#if lnx_occlusion_culling
|
||||
public var occlusionVisible = true;
|
||||
public var shadowOcclusionVisible = true;
|
||||
#end
|
||||
var shadowCameraCulled: Bool = false;
|
||||
var shadowCameraCullFrame: Int = -1;
|
||||
var shadowCameraCullCamera: CameraObject = null;
|
||||
var shadowCameraCullLight: LightObject = null;
|
||||
public var skip_context: String = null; // Do not draw this context
|
||||
public var force_context: String = null; // Draw only this context
|
||||
static var lastPipeline: PipelineState = null;
|
||||
|
||||
var cachedMats: Map<String, Vector<MaterialData>> = null;
|
||||
var cachedMaterialContexts: Map<String, Array<MaterialContext>> = null;
|
||||
var cachedShaderContexts: Map<String, Array<ShaderContext>> = null;
|
||||
#if lnx_morph_target
|
||||
public var morphTarget: MorphTarget = null;
|
||||
#end
|
||||
public var vertexGroups: Map<String, Array<Vec4>> = null;
|
||||
|
||||
#if lnx_veloc
|
||||
public var prevMatrix = Mat4.identity();
|
||||
@ -48,6 +59,9 @@ class MeshObject extends Object {
|
||||
this.materials = materials;
|
||||
setData(data);
|
||||
Scene.active.meshes.push(this);
|
||||
#if lnx_occlusion_culling
|
||||
if (iron.culling.CullSystem.active != null) iron.culling.CullSystem.active.markDirty();
|
||||
#end
|
||||
}
|
||||
|
||||
public function setData(data: MeshData) {
|
||||
@ -96,6 +110,9 @@ class MeshObject extends Object {
|
||||
if (activeTilesheet != null) activeTilesheet.remove();
|
||||
if (tilesheets != null) { for (ts in tilesheets) { ts.remove(); } tilesheets = null; }
|
||||
if (Scene.active != null) Scene.active.meshes.remove(this);
|
||||
#if lnx_occlusion_culling
|
||||
if (iron.culling.CullSystem.active != null) iron.culling.CullSystem.active.markDirty();
|
||||
#end
|
||||
#if (rp_renderer == "Deferred")
|
||||
if (Scene.active != null) Scene.active.markMaterialParamsDirty();
|
||||
#end
|
||||
@ -196,7 +213,7 @@ class MeshObject extends Object {
|
||||
var isShadow = context == "shadowmap";
|
||||
if (camera == null) return false;
|
||||
|
||||
if (camera.data.raw.frustum_culling && frustumCulling) {
|
||||
if (camera.data.raw.frustum_culling && frustumCulling && RenderPath.active != null && RenderPath.active.frustumCulling) {
|
||||
// Scale radius for skinned mesh and particle system
|
||||
// TODO: define skin & particle bounds
|
||||
var radiusScale = data.isSkinned ? 2.0 : 1.0;
|
||||
@ -211,7 +228,8 @@ class MeshObject extends Object {
|
||||
var frustumPlanes = isShadow ? light.frustumPlanes : camera.frustumPlanes;
|
||||
|
||||
if (isShadow && light.data.raw.type != "sun") { // Non-sun light bounds intersect camera frustum
|
||||
light.transform.radius = light.data.raw.far_plane;
|
||||
var farPlane: kha.FastFloat = light.data.raw.far_plane;
|
||||
light.transform.radius = farPlane * 1.25;
|
||||
if (!CameraObject.sphereInFrustum(camera.frustumPlanes, light.transform)) {
|
||||
return setCulled(isShadow, true);
|
||||
}
|
||||
@ -220,10 +238,40 @@ class MeshObject extends Object {
|
||||
if (!CameraObject.sphereInFrustum(frustumPlanes, transform, radiusScale)) {
|
||||
return setCulled(isShadow, true);
|
||||
}
|
||||
}
|
||||
|
||||
//culled = false;
|
||||
//return culled;
|
||||
#if rp_shadowmap
|
||||
if (isShadow && light.data.raw.type == "sun") {
|
||||
var frame = RenderPath.active.frame;
|
||||
if (shadowCameraCullFrame == frame && shadowCameraCullCamera == camera &&
|
||||
shadowCameraCullLight == light) {
|
||||
if (shadowCameraCulled) return setCulled(isShadow, true);
|
||||
} else {
|
||||
shadowCameraCullFrame = frame;
|
||||
shadowCameraCullCamera = camera;
|
||||
shadowCameraCullLight = light;
|
||||
shadowCameraCulled = false;
|
||||
var ldx = -light.V._02;
|
||||
var ldy = -light.V._12;
|
||||
var ldz = -light.V._22;
|
||||
var radius = transform.radius * radiusScale;
|
||||
var cx = transform.worldx();
|
||||
var cy = transform.worldy();
|
||||
var cz = transform.worldz();
|
||||
var range = camera.data.raw.far_plane * (LightObject.cascadeBounds * 4.0);
|
||||
var margin = radius * 0.1;
|
||||
for (plane in camera.frustumPlanes) {
|
||||
var reach = plane.normal.x * cx + plane.normal.y * cy + plane.normal.z * cz + plane.constant + radius + margin;
|
||||
var dot = plane.normal.x * ldx + plane.normal.y * ldy + plane.normal.z * ldz;
|
||||
if (dot > 0) reach += dot * range;
|
||||
if (reach < 0) {
|
||||
shadowCameraCulled = true;
|
||||
return setCulled(isShadow, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
return setCulled(isShadow, false);
|
||||
}
|
||||
|
||||
@ -253,6 +301,12 @@ class MeshObject extends Object {
|
||||
}
|
||||
}
|
||||
|
||||
static function matsEqual(a: Vector<MaterialData>, b: Vector<MaterialData>): Bool {
|
||||
if (a == null || b == null || a.length != b.length) return false;
|
||||
for (i in 0...a.length) if (a[i] != b[i]) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function render(g: Graphics, context: String, bindParams: Array<String>) {
|
||||
if (data == null || !data.geom.ready) return; // Data not yet streamed
|
||||
if (!visible) return; // Skip render if object is hidden
|
||||
@ -293,6 +347,7 @@ class MeshObject extends Object {
|
||||
// Get lod
|
||||
var mats = materials;
|
||||
var lod = this;
|
||||
var isShadowCtx = context == "shadowmap" || context == "shadowmap_transparent";
|
||||
if (raw != null && raw.lods != null && raw.lods.length > 0) {
|
||||
computeScreenSize(Scene.active.camera);
|
||||
initLods();
|
||||
@ -317,13 +372,29 @@ class MeshObject extends Object {
|
||||
if (isLodMaterial() && !validContext(mats, context)) return;
|
||||
|
||||
// Get context
|
||||
var materialContexts: Array<MaterialContext> = [];
|
||||
var shaderContexts: Array<ShaderContext> = [];
|
||||
getContexts(context, mats, materialContexts, shaderContexts);
|
||||
var materialContexts: Array<MaterialContext> = null;
|
||||
var shaderContexts: Array<ShaderContext> = null;
|
||||
if (cachedMaterialContexts != null && matsEqual(cachedMats.get(context), mats)) {
|
||||
materialContexts = cachedMaterialContexts.get(context);
|
||||
shaderContexts = cachedShaderContexts.get(context);
|
||||
}
|
||||
if (materialContexts == null) {
|
||||
materialContexts = [];
|
||||
shaderContexts = [];
|
||||
getContexts(context, mats, materialContexts, shaderContexts);
|
||||
if (cachedMaterialContexts == null) {
|
||||
cachedMats = new Map();
|
||||
cachedMaterialContexts = new Map();
|
||||
cachedShaderContexts = new Map();
|
||||
}
|
||||
cachedMats.set(context, mats);
|
||||
cachedMaterialContexts.set(context, materialContexts);
|
||||
cachedShaderContexts.set(context, shaderContexts);
|
||||
}
|
||||
|
||||
Uniforms.posUnpack = data.scalePos;
|
||||
Uniforms.texUnpack = data.scaleTex;
|
||||
transform.update();
|
||||
if (!isShadowCtx) transform.update();
|
||||
|
||||
// Render mesh
|
||||
var ldata = lod.data;
|
||||
|
||||
@ -15,6 +15,7 @@ class Object {
|
||||
public var transform: Transform;
|
||||
public var constraints: Array<Constraint> = null;
|
||||
public var traits: Array<Trait> = [];
|
||||
public var vertexGroups: Map<String, Array<iron.math.Vec4>> = null;
|
||||
|
||||
public var parent: Object = null;
|
||||
public var children: Array<Object> = [];
|
||||
|
||||
@ -17,6 +17,7 @@ class ObjectAnimation extends Animation {
|
||||
public var oactions: Array<TSceneFormat>;
|
||||
var oaction: TObj;
|
||||
var s0: FastFloat = 0.0;
|
||||
var delta: FastFloat = 0;
|
||||
var bezierFrameIndex = -1;
|
||||
|
||||
var updateAnimation: Map<String, FastFloat> -> Void;
|
||||
@ -26,7 +27,11 @@ class ObjectAnimation extends Animation {
|
||||
public var transformMap: Map<String, FastFloat>;
|
||||
|
||||
var defaultSampler: ActionSampler = null;
|
||||
static inline var DEFAULT_SAMPLER_ID = "__object_default_action__";
|
||||
var playSamplers: Map<String, ActionSampler> = [];
|
||||
var transitionSampler: ActionSampler = null;
|
||||
var transitionMap: Map<String, FastFloat> = null;
|
||||
var transitionTime: FastFloat = 0.0;
|
||||
var transitionDuration: FastFloat = 0.0;
|
||||
|
||||
public static var trackNames: Array<String> = [ "xloc", "yloc", "zloc",
|
||||
"xrot", "yrot", "zrot",
|
||||
@ -67,27 +72,59 @@ class ObjectAnimation extends Animation {
|
||||
oaction = getAction(this.action);
|
||||
if (oaction != null) {
|
||||
isSampled = oaction.sampled != null && oaction.sampled;
|
||||
if (defaultSampler != null) {
|
||||
deRegisterAction(DEFAULT_SAMPLER_ID);
|
||||
var sampler = playSamplers.get(this.action);
|
||||
if (sampler == null) {
|
||||
if (activeActions != null) sampler = activeActions.get(this.action);
|
||||
if (sampler == null) sampler = new ActionSampler(this.action);
|
||||
playSamplers.set(this.action, sampler);
|
||||
}
|
||||
var callbacks = onComplete != null ? [onComplete] : null;
|
||||
defaultSampler = new ActionSampler(this.action, speed, loop, false, callbacks);
|
||||
registerAction(DEFAULT_SAMPLER_ID, defaultSampler);
|
||||
if (paused) defaultSampler.paused = true;
|
||||
if (transitionSampler != null) {
|
||||
transitionSampler.paused = true;
|
||||
transitionSampler = null;
|
||||
}
|
||||
if (blendTime > 0 && defaultSampler != null && defaultSampler != sampler) {
|
||||
transitionSampler = defaultSampler;
|
||||
transitionMap = initTransformMap();
|
||||
transitionTime = 0.0;
|
||||
transitionDuration = blendTime;
|
||||
}
|
||||
else if (defaultSampler != null) defaultSampler.paused = true;
|
||||
defaultSampler = sampler;
|
||||
sampler.speed = speed;
|
||||
sampler.loop = loop;
|
||||
sampler.onComplete = onComplete != null ? [onComplete] : null;
|
||||
sampler.restartAction();
|
||||
registerAction(this.action, sampler);
|
||||
updateAnimation = function(map: Map<String, FastFloat>) {
|
||||
sampleAction(defaultSampler, map);
|
||||
if (transitionSampler != null) {
|
||||
sampleAction(transitionSampler, transitionMap);
|
||||
sampleAction(sampler, map);
|
||||
var f = transitionTime / transitionDuration;
|
||||
if (f > 1.0) f = 1.0;
|
||||
f = f * f * (3.0 - 2.0 * f); // Smoothstep
|
||||
blendActionObject(transitionMap, map, map, f);
|
||||
transitionTime += delta;
|
||||
if (transitionTime >= transitionDuration) {
|
||||
transitionSampler.paused = true;
|
||||
transitionSampler = null;
|
||||
}
|
||||
}
|
||||
else sampleAction(sampler, map);
|
||||
};
|
||||
}
|
||||
else {
|
||||
if (defaultSampler != null) {
|
||||
deRegisterAction(DEFAULT_SAMPLER_ID);
|
||||
defaultSampler = null;
|
||||
if (transitionSampler != null) {
|
||||
transitionSampler.paused = true;
|
||||
transitionSampler = null;
|
||||
}
|
||||
if (defaultSampler != null) defaultSampler.paused = true;
|
||||
defaultSampler = null;
|
||||
updateAnimation = null;
|
||||
}
|
||||
}
|
||||
|
||||
override public function update(delta: FastFloat) {
|
||||
this.delta = delta;
|
||||
if (!object.visible || object.culled) return;
|
||||
|
||||
#if lnx_debug
|
||||
@ -98,7 +135,6 @@ class ObjectAnimation extends Animation {
|
||||
transformMap = initTransformMap();
|
||||
|
||||
super.update(delta);
|
||||
if (defaultSampler != null) defaultSampler.paused = paused;
|
||||
if (paused) return;
|
||||
if (updateAnimation == null) return;
|
||||
if (!isSkinned) updateObjectAnimation();
|
||||
@ -160,11 +196,13 @@ class ObjectAnimation extends Animation {
|
||||
|
||||
var ti = sampler.offset;
|
||||
//ti = ti < 0 ? 1 : ti;
|
||||
var tj = ti + sign;
|
||||
if (tj < 0 || tj >= track.frames.length) tj = ti;
|
||||
|
||||
var t1 = track.frames[ti] * frameTime;
|
||||
var t2 = track.frames[ti + sign] * frameTime;
|
||||
var t2 = track.frames[tj] * frameTime;
|
||||
var v1 = track.values[ti];
|
||||
var v2 = track.values[ti + sign];
|
||||
var v2 = track.values[tj];
|
||||
|
||||
var value = interpolateLinear(t, t1, t2, v1, v2);
|
||||
|
||||
@ -202,6 +240,7 @@ class ObjectAnimation extends Animation {
|
||||
}
|
||||
|
||||
inline function interpolateLinear(t: FastFloat, t1: FastFloat, t2: FastFloat, v1: FastFloat, v2: FastFloat): Null<FastFloat> {
|
||||
if (t2 == t1) return v1;
|
||||
var s = (t - t1) / (t2 - t1);
|
||||
return (1.0 - s) * v1 + s * v2;
|
||||
}
|
||||
|
||||
@ -131,13 +131,20 @@ class Transform {
|
||||
dscale = new Vec4().set(1, 1, 1);
|
||||
}
|
||||
|
||||
static var _cdLoc = new Vec4();
|
||||
static var _cdRot = new Quat();
|
||||
static var _cdScale = new Vec4();
|
||||
|
||||
function composeDelta() {
|
||||
// Delta transform
|
||||
dloc.addvecs(loc, dloc);
|
||||
dscale.addvecs(dscale, scale);
|
||||
drot.fromEuler(_deulerX, _deulerY, _deulerZ);
|
||||
drot.multquats(rot, drot);
|
||||
local.compose(dloc, drot, dscale);
|
||||
// Delta transform: additive location, multiplicative rotation and scale
|
||||
_cdLoc.addvecs(loc, dloc);
|
||||
_cdScale.x = scale.x * dscale.x;
|
||||
_cdScale.y = scale.y * dscale.y;
|
||||
_cdScale.z = scale.z * dscale.z;
|
||||
_cdRot.fromEuler(_deulerX, _deulerY, _deulerZ);
|
||||
_cdRot.multquats(_cdRot, drot);
|
||||
_cdRot.multquats(rot, _cdRot);
|
||||
local.compose(_cdLoc, _cdRot, _cdScale);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -166,6 +166,7 @@ class LnxPack {
|
||||
case "anim": TAnimation;
|
||||
case "tracks": TTrack;
|
||||
case "morph_target": TMorphTarget;
|
||||
case "occluder_proxy": TOccluderProxy;
|
||||
case "vertex_groups": TVertex_groups;
|
||||
case "tilesheet": TTilesheetData;
|
||||
case "events": TTilesheetEvent;
|
||||
|
||||
@ -38,14 +38,14 @@ class Time {
|
||||
static var normalModeLogged: Bool = false;
|
||||
#end
|
||||
|
||||
static var lastTime = 0.0;
|
||||
static var lastTime = -1.0;
|
||||
static var _delta = 0.0;
|
||||
public static var delta(get, never): Float;
|
||||
static function get_delta(): Float {
|
||||
return _delta;
|
||||
}
|
||||
|
||||
static var lastRenderTime = 0.0;
|
||||
static var lastRenderTime = -1.0;
|
||||
static var _renderDelta = 0.0;
|
||||
public static var renderDelta(get, never): Float;
|
||||
static function get_renderDelta(): Float {
|
||||
@ -79,12 +79,14 @@ class Time {
|
||||
}
|
||||
#end
|
||||
|
||||
_delta = realTime() - lastTime;
|
||||
lastTime = realTime();
|
||||
var now = realTime();
|
||||
_delta = lastTime < 0.0 ? 0.0 : now - lastTime;
|
||||
lastTime = now;
|
||||
}
|
||||
|
||||
public static function render() {
|
||||
_renderDelta = realTime() - lastRenderTime;
|
||||
lastRenderTime = realTime();
|
||||
var now = realTime();
|
||||
_renderDelta = lastRenderTime < 0.0 ? 0.0 : now - lastRenderTime;
|
||||
lastRenderTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.system.Event;
|
||||
#if js
|
||||
import leenkx.network.Leenkx;
|
||||
#end
|
||||
import iron.object.Object;
|
||||
|
||||
class AddTorrentNode extends LogicNode {
|
||||
public var title: String;
|
||||
public var values: Array<Dynamic>;
|
||||
public var net_Url: String;
|
||||
public var data: Dynamic;
|
||||
public var runner: Bool = false;
|
||||
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
public function promiseResult(connection,torrentid,net_Url){
|
||||
#if js
|
||||
if (runner == false){
|
||||
values = [];
|
||||
var script = 'var torrentId = "' + torrentid + '";
|
||||
lx_' + net_Url +'.wt.add(torrentId, function (torrent) {
|
||||
leenkx.network.Leenkx.torrent.set("' + net_Url +'", torrent.files);
|
||||
torrent.on("done", function () {
|
||||
leenkx.network.Leenkx.connections.h["' + net_Url +'"].ontorrentdone();
|
||||
})
|
||||
});
|
||||
';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runner = true;
|
||||
promiseResult(connection,torrentid,net_Url);
|
||||
return;
|
||||
}else{
|
||||
try{
|
||||
values = leenkx.network.Leenkx.torrent.get(net_Url);
|
||||
if(values.length > 0){
|
||||
runOutput(0);
|
||||
runner = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(error){
|
||||
haxe.Timer.delay(function () {
|
||||
promiseResult(connection,torrentid,net_Url);
|
||||
return;
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
|
||||
override function run(from:Int) {
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
var torrentid = inputs[2].get();
|
||||
if (torrentid == null) return;
|
||||
#if js
|
||||
net_Url = connection._url;
|
||||
promiseResult(connection,torrentid,net_Url);
|
||||
#end
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
#if js
|
||||
return switch (from) {
|
||||
case 1: Leenkx.id.get(net_Url);
|
||||
case 2: leenkx.network.Leenkx.torrent.get(net_Url);
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
|
||||
}
|
||||
@ -35,13 +35,13 @@ class AnimActionNode extends LogicNode {
|
||||
if(object.animation == null) {
|
||||
#if lnx_skin
|
||||
animationBone = object.getBoneAnimation(object.uid);
|
||||
animationBone.registerAction(property0, sampler);
|
||||
animationBone.registerAction(sampler.action, sampler);
|
||||
func = sampleBonaAction;
|
||||
#end
|
||||
}
|
||||
else{
|
||||
animationObject = cast(object.animation, ObjectAnimation);
|
||||
animationObject.registerAction(property0, sampler);
|
||||
animationObject.registerAction(sampler.action, sampler);
|
||||
func = sampleObjectAction;
|
||||
}
|
||||
|
||||
|
||||
@ -61,14 +61,16 @@ class BlendActionNode extends LogicNode {
|
||||
return;
|
||||
}
|
||||
if(factor > 0.95) {
|
||||
|
||||
inputs[2].get()(animMats);
|
||||
var action2 = inputs[2].get();
|
||||
if (action2 != null) action2(animMats); else inputs[1].get()(animMats);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
inputs[1].get()(animMats);
|
||||
inputs[2].get()(tempMats);
|
||||
var action2 = inputs[2].get();
|
||||
if (action2 == null) return;
|
||||
action2(tempMats);
|
||||
animationBone.blendAction(animMats, tempMats, animMats, factor, boneLayer);
|
||||
|
||||
}
|
||||
|
||||
@ -1,131 +1,170 @@
|
||||
package leenkx.logicnode;
|
||||
import iron.object.Object;
|
||||
#if js
|
||||
import leenkx.network.Types;
|
||||
import leenkx.network.Util;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.LeenkxClient;
|
||||
import leenkx.network.torrent.TorrentClient;
|
||||
import leenkx.network.torrent.peer.RtcPeerPool;
|
||||
#end
|
||||
import leenkx.system.Event;
|
||||
|
||||
|
||||
class CreateLeenkxNode extends LogicNode {
|
||||
public var property0: String;
|
||||
public var property1: Bool;
|
||||
public var property2: String;
|
||||
public var property3: String;
|
||||
public var net_Url: String;
|
||||
public var data: Dynamic;
|
||||
public var client: Dynamic;
|
||||
public var value: String;
|
||||
public var listener: TEvent = null;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if js
|
||||
net_Url = inputs[1].get();
|
||||
if (net_Url == null) return;
|
||||
if(Leenkx.connections[net_Url] == null){
|
||||
var object = tree.object;
|
||||
value = Leenkx.onLoadEvent;
|
||||
listener = Event.add(value, onEvent, tree.object.uid);
|
||||
client = new leenkx.network.Leenkx(net_Url, object);
|
||||
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
function onEvent() {
|
||||
#if js
|
||||
var lnxjs:Dynamic = js.Lib.global;
|
||||
var lnxCxNew = lnxjs.lnxNew;
|
||||
|
||||
if (lnxCxNew == null) {
|
||||
trace("ERROR: lnxNew not available");
|
||||
return;
|
||||
#if lnx_torrent
|
||||
net_Url = inputs[1].get();
|
||||
if (net_Url == null) return;
|
||||
if (Leenkx.connections[net_Url] == null) {
|
||||
value = Leenkx.onLoadEvent;
|
||||
listener = Event.add(value, onEvent, tree.object.uid);
|
||||
new leenkx.network.Leenkx(net_Url, tree.object);
|
||||
}
|
||||
|
||||
var lnxCn:Dynamic = lnxCxNew(net_Url);
|
||||
|
||||
lnxCn.on("connections", function(c) {
|
||||
leenkx.network.Leenkx.data.set(net_Url, c + 1);
|
||||
leenkx.network.Leenkx.connections[net_Url].onconnections();
|
||||
});
|
||||
lnxCn.on("message", function(address, message) {
|
||||
leenkx.network.Leenkx.data.set(net_Url, message);
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].onmessage();
|
||||
});
|
||||
lnxCn.on("seen", function(address) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].onseen();
|
||||
});
|
||||
lnxCn.on("left", function(address) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].onleft();
|
||||
});
|
||||
lnxCn.on("server", function(address) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].onserver();
|
||||
});
|
||||
lnxCn.on("ping", function(address) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].onping();
|
||||
});
|
||||
lnxCn.on("timeout", function(address) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].ontimeout();
|
||||
});
|
||||
lnxCn.on("rpc", function(address, call, args, nonce) {
|
||||
leenkx.network.Leenkx.data.set(net_Url, call);
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
call(args);
|
||||
leenkx.network.Leenkx.connections[net_Url].onrpc();
|
||||
});
|
||||
lnxCn.on("rpc-response", function(address, nonce, response) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, address);
|
||||
leenkx.network.Leenkx.connections[net_Url].onrpcresponse();
|
||||
});
|
||||
lnxCn.on("wireleft", function(wirecount, wire) {
|
||||
leenkx.network.Leenkx.data.set(net_Url, wirecount);
|
||||
leenkx.network.Leenkx.id.set(net_Url, wire.peerId);
|
||||
leenkx.network.Leenkx.connections[net_Url].onwireleft();
|
||||
});
|
||||
lnxCn.on("wireseen", function(wirecount, wire) {
|
||||
leenkx.network.Leenkx.data.set(net_Url, wirecount);
|
||||
leenkx.network.Leenkx.id.set(net_Url, wire.peerId);
|
||||
leenkx.network.Leenkx.connections[net_Url].onwireseen();
|
||||
});
|
||||
lnxCn.on("torrent", function(identifier, torrent) {
|
||||
leenkx.network.Leenkx.data.set(net_Url, torrent);
|
||||
leenkx.network.Leenkx.id.set(net_Url, identifier);
|
||||
leenkx.network.Leenkx.connections[net_Url].ontorrent();
|
||||
});
|
||||
lnxCn.on("tracker", function(identifier) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, identifier);
|
||||
leenkx.network.Leenkx.connections[net_Url].ontracker();
|
||||
});
|
||||
lnxCn.on("announce", function(identifier) {
|
||||
leenkx.network.Leenkx.id.set(net_Url, identifier);
|
||||
leenkx.network.Leenkx.connections[net_Url].onannounce();
|
||||
});
|
||||
|
||||
Reflect.setField(lnxjs, "lnx_" + net_Url, lnxCn);
|
||||
Leenkx.connections[net_Url].client = lnxCn;
|
||||
|
||||
var script = 'globalThis.addEventListener("beforeunload", function (e) {
|
||||
leenkx.network.Leenkx.connections.h["' + net_Url + '"].client.destroy();
|
||||
delete e["returnValue"];
|
||||
});';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
|
||||
runOutput(0);
|
||||
#end
|
||||
}
|
||||
|
||||
function onEvent() {
|
||||
if (listener != null) {
|
||||
Event.removeListener(listener);
|
||||
listener = null;
|
||||
}
|
||||
#if lnx_torrent
|
||||
var sock = Leenkx.connections[net_Url];
|
||||
if (sock == null) return;
|
||||
if (sock.client != null) {
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
var opts:leenkx.network.torrent.Types.LeenkxOptions = {deferJoin: true};
|
||||
var nextInput = 2;
|
||||
if (property0 == "datachannel") {
|
||||
opts.transport = "datachannel";
|
||||
var imode = property2 != null ? property2 : "inherit";
|
||||
if (imode != "inherit") {
|
||||
if (inputs.length > nextInput) {
|
||||
var ice = TorrentClient.trackerUrls(inputs[nextInput].get());
|
||||
if (ice != null) {
|
||||
opts.iceServers = imode == "override" ? ice : RtcPeerPool.defaultIceServers.concat(ice);
|
||||
}
|
||||
}
|
||||
nextInput++;
|
||||
}
|
||||
}
|
||||
var tmode = property3 != null ? property3 : "inherit";
|
||||
var trackers:Array<String> = null;
|
||||
if (tmode != "inherit" && inputs.length > nextInput) {
|
||||
trackers = TorrentClient.trackerUrls(inputs[nextInput].get());
|
||||
}
|
||||
opts.announce = tmode == "override" && trackers != null ? trackers
|
||||
: trackers != null ? TorrentClient.defaultTrackers.concat(trackers)
|
||||
: TorrentClient.defaultTrackers;
|
||||
var lnxCn = new LeenkxClient(net_Url, opts);
|
||||
lnxCn.isRelay = property1 == true;
|
||||
|
||||
lnxCn.onOpen = function() {
|
||||
Leenkx.data.set(net_Url, lnxCn.lastPeerIndex);
|
||||
Leenkx.id.set(net_Url, lnxCn.lastPeerId);
|
||||
sock.onopen();
|
||||
};
|
||||
lnxCn.onConnections = function(c) {
|
||||
Leenkx.data.set(net_Url, c + 1);
|
||||
sock.onconnections();
|
||||
};
|
||||
lnxCn.onMessage = function(address, message, nonce) {
|
||||
Leenkx.data.set(net_Url, message);
|
||||
Leenkx.id.set(net_Url, address);
|
||||
sock.onmessage();
|
||||
};
|
||||
lnxCn.onSeen = function(address) {
|
||||
Leenkx.id.set(net_Url, address);
|
||||
sock.onseen();
|
||||
};
|
||||
lnxCn.onLeft = function(address) {
|
||||
Leenkx.id.set(net_Url, address);
|
||||
sock.onleft();
|
||||
sock.onclose();
|
||||
};
|
||||
lnxCn.onServer = function(address) {
|
||||
Leenkx.id.set(net_Url, address);
|
||||
sock.onserver();
|
||||
};
|
||||
lnxCn.onPing = function(address) {
|
||||
Leenkx.id.set(net_Url, address);
|
||||
sock.onping();
|
||||
};
|
||||
lnxCn.onTimeout = function(address) {
|
||||
Leenkx.id.set(net_Url, address);
|
||||
sock.ontimeout();
|
||||
sock.onclose();
|
||||
};
|
||||
lnxCn.onWireLeft = function(wirecount, wire) {
|
||||
Leenkx.data.set(net_Url, wirecount);
|
||||
if (wire != null && wire.peerId != null) {
|
||||
Leenkx.id.set(net_Url, wire.peerId);
|
||||
}
|
||||
sock.onwireleft();
|
||||
};
|
||||
lnxCn.onWireSeen = function(wirecount, wire) {
|
||||
Leenkx.data.set(net_Url, wirecount);
|
||||
if (wire != null && wire.peerId != null) {
|
||||
Leenkx.id.set(net_Url, wire.peerId);
|
||||
}
|
||||
sock.onwireseen();
|
||||
};
|
||||
lnxCn.onTorrent = function(identifier, torrent) {
|
||||
Leenkx.data.set(net_Url, torrent);
|
||||
Leenkx.id.set(net_Url, identifier);
|
||||
sock.ontorrent();
|
||||
};
|
||||
lnxCn.onTracker = function(identifier, tracker) {
|
||||
Leenkx.id.set(net_Url, identifier);
|
||||
sock.ontracker();
|
||||
};
|
||||
lnxCn.onAnnounce = function(identifier) {
|
||||
Leenkx.id.set(net_Url, identifier);
|
||||
sock.onannounce();
|
||||
};
|
||||
lnxCn.onError = function(msg) {
|
||||
Leenkx.data.set(net_Url, msg);
|
||||
sock.onerror();
|
||||
};
|
||||
lnxCn.onTorrentAdded = function(t) {
|
||||
Leenkx.torrent.set(net_Url, t);
|
||||
Leenkx.file.set(net_Url, t.files);
|
||||
Leenkx.data.set(net_Url, t);
|
||||
sock.ontorrentadded();
|
||||
};
|
||||
lnxCn.onTorrentDone = function(t) {
|
||||
Leenkx.data.set(net_Url, t);
|
||||
sock.ontorrentdone();
|
||||
};
|
||||
|
||||
sock.client = lnxCn;
|
||||
|
||||
lnxCn.connect();
|
||||
#if (js && !kha_krom)
|
||||
js.Browser.window.addEventListener("beforeunload", function(e) {
|
||||
lnxCn.destroy();
|
||||
});
|
||||
#end
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
#if js
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: Leenkx.connections[net_Url];
|
||||
case 2: Leenkx.id.get(net_Url);
|
||||
|
||||
@ -1,43 +1,100 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.system.Event;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import iron.object.Object;
|
||||
import leenkx.network.torrent.Magnet;
|
||||
import leenkx.network.torrent.TorrentClient;
|
||||
#end
|
||||
|
||||
|
||||
class CreateTorrentNode extends LogicNode {
|
||||
public var torrent: Dynamic;
|
||||
public var net_Url: String;
|
||||
public var meta:Dynamic;
|
||||
public var metaBytes:Dynamic;
|
||||
public var magnetUri:Dynamic;
|
||||
public var infoHashHex:String;
|
||||
public var property0:String;
|
||||
public var property1:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
meta = null;
|
||||
metaBytes = null;
|
||||
magnetUri = null;
|
||||
infoHashHex = null;
|
||||
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
var data = inputs[2].get();
|
||||
if (data == null) return;
|
||||
net_Url = connection._url;
|
||||
var data = inputs[1].get();
|
||||
if (data == null) return;
|
||||
var mode = property1 != null ? property1 : "file";
|
||||
var opts:Dynamic = {};
|
||||
var mv = property0 != null ? Std.parseInt(property0) : null;
|
||||
if (mv != null) Reflect.setField(opts, "metaVersion", mv);
|
||||
field(2, opts, "name");
|
||||
field(3, opts, "comment");
|
||||
field(4, opts, "createdBy");
|
||||
field(5, opts, "source");
|
||||
field(6, opts, "announce");
|
||||
field(7, opts, "private");
|
||||
field(8, opts, "pieceLength");
|
||||
field(9, opts, "urlList");
|
||||
|
||||
var script = 'var file = ' + data + ';
|
||||
lx_' + net_Url +'.wt.add(file, function (torrent) {
|
||||
leenkx.network.Leenkx.file.set("' + net_Url +'", torrent.files);
|
||||
torrent.on("done", function () {
|
||||
leenkx.network.Leenkx.connections.h["' + net_Url +'"].ontorrentdone();
|
||||
});
|
||||
});
|
||||
';
|
||||
if (mode == "folder") {
|
||||
#if sys
|
||||
data = TorrentClient.readFolder(Std.string(data));
|
||||
var first:Dynamic = data[0];
|
||||
if (first != null && Reflect.field(opts, "name") == null) {
|
||||
var p = Std.string(first.path);
|
||||
var i = p.indexOf("/");
|
||||
Reflect.setField(opts, "name", p.substring(0, i >= 0 ? i : p.length));
|
||||
}
|
||||
#else
|
||||
trace("Folder mode requires a sys target";
|
||||
#end
|
||||
}
|
||||
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
var m;
|
||||
try {
|
||||
m = TorrentClient.buildMeta(data, opts);
|
||||
} catch(e:Dynamic) {
|
||||
trace(e);
|
||||
return;
|
||||
}
|
||||
meta = m;
|
||||
metaBytes = m.toTorrentBytes();
|
||||
infoHashHex = m.infoHashHex != null ? m.infoHashHex : m.infoHashV2Hex;
|
||||
magnetUri = Magnet.build(m.infoHash, m.name, m.announce, m.urlList, m.infoHashV2);
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
#if lnx_torrent
|
||||
function field(i:Int, opts:Dynamic, name:String):Void {
|
||||
if (i >= inputs.length) return;
|
||||
var v:Dynamic = inputs[i].get();
|
||||
if (v == null || Std.string(v) == "") {
|
||||
return;
|
||||
}
|
||||
if (name == "announce") {
|
||||
v = TorrentClient.trackerUrls(v);
|
||||
if (v == null) return;
|
||||
}
|
||||
Reflect.setField(opts, name, v);
|
||||
}
|
||||
#end
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
return switch (from) {
|
||||
case 1: Leenkx.file.get(net_Url);
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
}
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: meta;
|
||||
case 2: metaBytes;
|
||||
case 3: infoHashHex;
|
||||
case 4: magnetUri;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
88
leenkx/Sources/leenkx/logicnode/CreateTrackerNode.hx
Normal file
88
leenkx/Sources/leenkx/logicnode/CreateTrackerNode.hx
Normal file
@ -0,0 +1,88 @@
|
||||
package leenkx.logicnode;
|
||||
#if (lnx_torrent && (sys || kha_krom))
|
||||
import leenkx.network.torrent.tracker.HttpTracker.HttpTrackerServer;
|
||||
import leenkx.network.torrent.tracker.UdpTracker.UdpTrackerServer;
|
||||
import leenkx.network.torrent.transport.WsTrackerClient.WsTrackerServer;
|
||||
#end
|
||||
#if (lnx_torrent && sys)
|
||||
import leenkx.network.torrent.transport.WsTrackerClient.WssTrackerServer;
|
||||
#end
|
||||
|
||||
class CreateTrackerNode extends LogicNode {
|
||||
public var property0:String;
|
||||
public var tracker:Dynamic;
|
||||
public var url:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
tracker = null;
|
||||
url = null;
|
||||
startServer();
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
function startServer():Void {
|
||||
#if (lnx_torrent && (sys || kha_krom))
|
||||
var hv = inputs[1].get();
|
||||
var host = hv != null ? Std.string(hv) : "127.0.0.1";
|
||||
if (host == "") host = "127.0.0.1";
|
||||
var pv = inputs[2].get();
|
||||
var port = pv != null ? Std.int(Std.parseFloat(Std.string(pv))) : 0;
|
||||
if (port <= 0) port = 6969;
|
||||
var scheme = property0 != null ? property0 : "udp";
|
||||
switch (scheme) {
|
||||
case "http":
|
||||
var s = new HttpTrackerServer();
|
||||
s.onError = function(e) trace(e);
|
||||
s.start(port);
|
||||
tracker = s;
|
||||
url = "http://" + host + ":" + port + "/announce";
|
||||
case "ws":
|
||||
var s = new WsTrackerServer(host, port);
|
||||
s.onError = function(e) trace(e);
|
||||
s.start();
|
||||
tracker = s;
|
||||
url = "ws://" + host + ":" + port;
|
||||
case "wss":
|
||||
#if sys
|
||||
var cert = inputs.length > 3 ? Std.string(inputs[3].get()) : null;
|
||||
var key = inputs.length > 4 ? Std.string(inputs[4].get()) : null;
|
||||
if (cert == null || cert == "" || key == null || key == "") {
|
||||
trace("wss server needs Cert File and Key File");
|
||||
return;
|
||||
}
|
||||
var s = new WssTrackerServer(host, port, cert, key);
|
||||
s.onError = function(e) trace(e);
|
||||
s.start();
|
||||
tracker = s;
|
||||
url = "wss://" + host + ":" + port;
|
||||
#else
|
||||
trace("wss server requires a sys target");
|
||||
#end
|
||||
default:
|
||||
var s = new UdpTrackerServer();
|
||||
s.onError = function(e) trace(e);
|
||||
s.start(port);
|
||||
tracker = s;
|
||||
url = "udp://" + host + ":" + port;
|
||||
}
|
||||
#else
|
||||
trace("server mode needs a sys or krom target");
|
||||
#end
|
||||
}
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: tracker;
|
||||
case 2: url;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
101
leenkx/Sources/leenkx/logicnode/DownloadTorrentNode.hx
Normal file
101
leenkx/Sources/leenkx/logicnode/DownloadTorrentNode.hx
Normal file
@ -0,0 +1,101 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.system.Event;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.TorrentClient;
|
||||
#end
|
||||
|
||||
class DownloadTorrentNode extends LogicNode {
|
||||
public var net_Url:String;
|
||||
public var torrent:Dynamic;
|
||||
public var property0:String;
|
||||
public var property1:String;
|
||||
var listener:TEvent = null;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
public function promiseResult(connection, torrentid, net_Url, ?opts:Dynamic) {
|
||||
#if lnx_torrent
|
||||
var socket = Leenkx.connections[net_Url];
|
||||
if (socket == null) {
|
||||
if (listener == null) {
|
||||
var self = this;
|
||||
listener = Event.add(Leenkx.onLoadEvent, function() {
|
||||
if (Leenkx.connections[net_Url] == null) return;
|
||||
if (self.listener != null) {
|
||||
Event.removeListener(self.listener);
|
||||
self.listener = null;
|
||||
}
|
||||
self.promiseResult(connection, torrentid, net_Url, opts);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
var client = socket.client;
|
||||
if (client == null) return;
|
||||
var self = this;
|
||||
client.add(torrentid, opts, function(torrent) {
|
||||
self.torrent = torrent;
|
||||
var onReady = function() {
|
||||
self.runOutput(0);
|
||||
};
|
||||
var onDone = function() {
|
||||
self.runOutput(4);
|
||||
};
|
||||
if (torrent.ready) onReady();
|
||||
else torrent.on("ready", onReady);
|
||||
if (torrent.done) onDone();
|
||||
else torrent.on("done", onDone);
|
||||
});
|
||||
#end
|
||||
}
|
||||
|
||||
function onRemove() {
|
||||
if (listener != null) {
|
||||
Event.removeListener(listener);
|
||||
listener = null;
|
||||
}
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
var v = inputs[2].get();
|
||||
if (v == null) return;
|
||||
#if lnx_torrent
|
||||
net_Url = connection._url;
|
||||
var torrentId:Dynamic = switch (property0 != null ? property0 : "magnet") {
|
||||
case "file": v;
|
||||
case "id": TorrentClient.torrentIdFor(v);
|
||||
default: Std.string(v);
|
||||
};
|
||||
var opts:Dynamic = null;
|
||||
var tmode = property1 != null ? property1 : "inherit";
|
||||
if (tmode != "inherit" && inputs.length > 3) {
|
||||
var announce = TorrentClient.trackerUrls(inputs[3].get());
|
||||
opts = {};
|
||||
if (announce != null) opts.announce = announce;
|
||||
if (tmode == "override") opts.announceOverride = true;
|
||||
}
|
||||
promiseResult(connection, torrentId, net_Url, opts);
|
||||
#end
|
||||
}
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
var t:leenkx.network.torrent.Torrent = torrent;
|
||||
return switch (from) {
|
||||
case 1: torrent;
|
||||
case 2: t != null && t.meta != null ? t.meta.name : null;
|
||||
case 3: t != null ? t.files : null;
|
||||
case 5: t != null ? t.infoHashHex : null;
|
||||
case 6: t != null ? t.magnetURI : null;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
83
leenkx/Sources/leenkx/logicnode/GetTorrentFileNode.hx
Normal file
83
leenkx/Sources/leenkx/logicnode/GetTorrentFileNode.hx
Normal file
@ -0,0 +1,83 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
import leenkx.network.torrent.TorrentFile;
|
||||
#end
|
||||
|
||||
class GetTorrentFileNode extends LogicNode {
|
||||
public var property0:String;
|
||||
public var file:Dynamic;
|
||||
var net_Url:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
file = null;
|
||||
var id = inputs[2].get();
|
||||
var match = inputs[3].get();
|
||||
if (id != null && match != null) {
|
||||
var connection = inputs[1].get();
|
||||
if (connection != null) net_Url = connection._url;
|
||||
var socket = net_Url != null ? Leenkx.connections[net_Url] : null;
|
||||
var client = socket != null ? socket.client : null;
|
||||
var t = client != null ? client.get(id) : (Std.isOfType(id, Torrent) ? id : null);
|
||||
if (t != null) file = findFile(t, match);
|
||||
}
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
#if lnx_torrent
|
||||
function findFile(t:Torrent, match:Dynamic):TorrentFile {
|
||||
var files = t.files;
|
||||
switch (property0) {
|
||||
case "index":
|
||||
var i = Std.parseInt(Std.string(match));
|
||||
if (i != null && i >= 0 && i < files.length) {
|
||||
return files[i];
|
||||
}
|
||||
case "name":
|
||||
var s = Std.string(match);
|
||||
for (f in files) {
|
||||
if (f.name == s) return f;
|
||||
}
|
||||
case "path":
|
||||
var s = Std.string(match);
|
||||
for (f in files) {
|
||||
if (f.path == s) return f;
|
||||
}
|
||||
default:
|
||||
var s = Std.string(match);
|
||||
if (!StringTools.startsWith(s, ".")) s = "." + s;
|
||||
for (f in files) {
|
||||
if (StringTools.endsWith(f.name, s)) return f;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
#end
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
var f:TorrentFile = file;
|
||||
return switch (from) {
|
||||
case 1: file;
|
||||
case 2: f != null ? f.name : null;
|
||||
case 3: f != null ? f.path : null;
|
||||
case 4: f != null ? f.length : 0;
|
||||
case 5: f != null ? f.type : null;
|
||||
case 6: f != null ? f.getBytes() : null;
|
||||
case 7: f != null;
|
||||
case 8: f != null ? f.downloaded : 0.0;
|
||||
case 9: f != null ? f.progress : 0.0;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.system.Event;
|
||||
import leenkx.network.Connect;
|
||||
import iron.object.Object;
|
||||
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.LeenkxClient;
|
||||
#end
|
||||
|
||||
class LeenkxCloseConnectionNode extends LogicNode {
|
||||
public var property0: String;
|
||||
@ -12,41 +12,28 @@ class LeenkxCloseConnectionNode extends LogicNode {
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
if (connection == null) return;
|
||||
var socket = Leenkx.connections[connection._url];
|
||||
if (socket == null) return;
|
||||
|
||||
if(property0 == "client") {
|
||||
var script = '
|
||||
for (p in leenkx.network.Leenkx.connections.h["' + connection._url + '"].client.torrent._peers){
|
||||
try{
|
||||
leenkx.network.Leenkx.id.set("' + connection._url + '",p);
|
||||
leenkx.network.Leenkx.connections.h["' + connection._url + '"].client.torrent._peers[p].conn._pc.close();
|
||||
leenkx.network.Leenkx.connections.h["' + connection._url + '"].client.torrent._peers[p].conn.destroy();
|
||||
leenkx.network.Leenkx.connections.h["' + connection._url + '"].onclose();
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}
|
||||
';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
} else {
|
||||
var script = '
|
||||
try{
|
||||
var lnxConn = leenkx.network.Leenkx.connections.h["' + connection._url + '"];
|
||||
if (lnxConn && lnxConn.client && lnxConn.client.torrent && lnxConn.client.torrent._peers) {
|
||||
for (var p in lnxConn.client.torrent._peers) {
|
||||
lnxConn.client.torrent._peers[p].conn._pc.close();
|
||||
lnxConn.client.torrent._peers[p].conn.destroy();
|
||||
leenkx.network.Leenkx.id.set("' + connection._url + '", p);
|
||||
lnxConn.onclose();
|
||||
}
|
||||
}
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
var lnxCn:LeenkxClient = socket.client;
|
||||
if (property0 == "peer") {
|
||||
var id:String = inputs.length > 2 ? inputs[2].get() : null;
|
||||
if (id != null && lnxCn != null) {
|
||||
lnxCn.peers.remove(id);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
if (lnxCn != null) {
|
||||
lnxCn.destroy();
|
||||
socket.client = null;
|
||||
}
|
||||
Leenkx.connections.remove(connection._url);
|
||||
socket.onclose();
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.system.Event;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import iron.object.Object;
|
||||
#end
|
||||
|
||||
class LeenkxEventNode extends LogicNode {
|
||||
public var property0: String;
|
||||
public var property1: String;
|
||||
|
||||
public var value: String;
|
||||
@ -14,90 +14,63 @@ class LeenkxEventNode extends LogicNode {
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
tree.notifyOnInit(init);
|
||||
tree.notifyOnRemove(onRemove);
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (property0 == "client") {
|
||||
net_Url = inputs[0].get();
|
||||
switch (property1) {
|
||||
case "onopen": value = Leenkx.onOpenEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onmessage": value = Leenkx.onMessageEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onerror": value = Leenkx.onErrorEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onclose": value = Leenkx.onCloseEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onseen": value = Leenkx.onSeenEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onserver": value = Leenkx.onServerEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onconnections": value = Leenkx.onConnectionsEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onping": value = Leenkx.onPingEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onleft": value = Leenkx.onLeftEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "ontimeout": value = Leenkx.onTimeOutEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onrpc": value = Leenkx.onRpcEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onrpcresponse": value = Leenkx.onRpcResponseEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onwireleft": value = Leenkx.onWireLeftEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onwireseen": value = Leenkx.onWireSeenEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "ontorrent": value = Leenkx.onTorrentEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "ontracker": value = Leenkx.onTrackerEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onannounce": value = Leenkx.onAnnounceEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
default: throw "Failed to set client event type.";
|
||||
}
|
||||
} else if (property0 == "host") {
|
||||
#if (sys || kha_krom)
|
||||
var net_Domain = inputs[0].get();
|
||||
var net_Port = inputs[1].get();
|
||||
net_Url = "ws://" + net_Domain + ":" + Std.string(net_Port);
|
||||
switch (property1) {
|
||||
case "onopen": value = Leenkx.onOpenEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onmessage": value = Leenkx.onMessageEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onerror": value = Leenkx.onErrorEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onclose": value = Leenkx.onCloseEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
default: throw "Failed to set host event type.";
|
||||
}
|
||||
#end
|
||||
} else if (property0 == "securehost"){
|
||||
#if (sys || kha_krom)
|
||||
var net_Domain = inputs[0].get();
|
||||
var net_Port = inputs[1].get();
|
||||
net_Url = "wss://" + net_Domain + ":" + Std.string(net_Port);
|
||||
switch (property1) {
|
||||
case "onopen": value = Leenkx.onOpenEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onmessage": value = Leenkx.onMessageEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onerror": value = Leenkx.onErrorEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
case "onclose": value = Leenkx.onCloseEvent;listener = Event.add(value, onEvent, tree.object.uid);
|
||||
default: throw "Failed to set host event type.";
|
||||
}
|
||||
#end
|
||||
#if lnx_torrent
|
||||
var u:Dynamic = inputs[0].get();
|
||||
net_Url = Std.isOfType(u, String) ? u : (u != null ? u._url : null);
|
||||
switch (property1) {
|
||||
case "onopen": value = Leenkx.onOpenEvent;
|
||||
case "onmessage": value = Leenkx.onMessageEvent;
|
||||
case "onerror": value = Leenkx.onErrorEvent;
|
||||
case "onclose": value = Leenkx.onCloseEvent;
|
||||
case "onseen": value = Leenkx.onSeenEvent;
|
||||
case "onserver": value = Leenkx.onServerEvent;
|
||||
case "onconnections": value = Leenkx.onConnectionsEvent;
|
||||
case "onping": value = Leenkx.onPingEvent;
|
||||
case "onleft": value = Leenkx.onLeftEvent;
|
||||
case "ontimeout": value = Leenkx.onTimeOutEvent;
|
||||
case "onwireleft": value = Leenkx.onWireLeftEvent;
|
||||
case "onwireseen": value = Leenkx.onWireSeenEvent;
|
||||
case "ontorrent": value = Leenkx.onTorrentEvent;
|
||||
case "ontorrentadded": value = Leenkx.onTorrentAddedEvent;
|
||||
case "ontracker": value = Leenkx.onTrackerEvent;
|
||||
case "onannounce": value = Leenkx.onAnnounceEvent;
|
||||
case "ontorrentdone": value = Leenkx.onTorrentDoneEvent;
|
||||
case "onload": value = Leenkx.onLoadEvent;
|
||||
default:
|
||||
trace("Failed to set client event type");
|
||||
}
|
||||
|
||||
|
||||
listener = Event.add(value, onEvent, tree.object.uid);
|
||||
#end
|
||||
}
|
||||
|
||||
function onEvent() {
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
if (property0 == "host") {
|
||||
return switch (from) {
|
||||
case 1: Leenkx.id.get(net_Url);
|
||||
case 2: Leenkx.data.get(net_Url);
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
}
|
||||
else if (property0 == "securehost") {
|
||||
return switch (from) {
|
||||
case 1: Leenkx.id.get(net_Url);
|
||||
case 2: Leenkx.data.get(net_Url);
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return switch (from) {
|
||||
case 1: Leenkx.id.get(net_Url);
|
||||
case 2: Leenkx.data.get(net_Url);
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
function onRemove() {
|
||||
if (listener != null) {
|
||||
Event.removeListener(listener);
|
||||
listener = null;
|
||||
}
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: Leenkx.id.get(net_Url);
|
||||
case 2: Leenkx.data.get(net_Url);
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.network.Connect;
|
||||
import leenkx.network.Buffer;
|
||||
import haxe.io.Bytes;
|
||||
import iron.object.Object;
|
||||
import iron.math.Vec4;
|
||||
import iron.math.Quat;
|
||||
import iron.math.Mat4;
|
||||
@ -22,140 +20,107 @@ class LeenkxMessageParserNode extends LogicNode {
|
||||
if (data == null) return;
|
||||
|
||||
api = inputs[1].get();
|
||||
if (api == null) api = "";
|
||||
|
||||
var buffer:Buffer;
|
||||
if (Std.isOfType(data, Buffer)) {
|
||||
buffer = cast data;
|
||||
} else {
|
||||
var rawBytes:Bytes;
|
||||
try {
|
||||
rawBytes = haxe.crypto.Base64.decode(Std.string(data));
|
||||
} catch(e) {
|
||||
trace("Base64 decode failed: " + e);
|
||||
return;
|
||||
}
|
||||
buffer = new Buffer();
|
||||
buffer.writeBytes(rawBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
var buffer = cast(data, Buffer);
|
||||
api = inputs[1].get();
|
||||
if(buffer.endsWith(api) == false){
|
||||
return;
|
||||
}
|
||||
**/
|
||||
var buffer = new Buffer();
|
||||
if(!StringTools.startsWith(data,api)){
|
||||
return;
|
||||
}
|
||||
switch (property0) {
|
||||
case "string":
|
||||
parsed = data.substr(api.length, data.length);
|
||||
runOutput(0);
|
||||
case "vector":
|
||||
var vec = data.substr(api.length, data.length);
|
||||
vec = StringTools.replace(vec, "(", "");
|
||||
vec = StringTools.replace(vec, ")", "");
|
||||
vec = StringTools.replace(vec, " ", "");
|
||||
vec = StringTools.trim(vec);
|
||||
var array = vec.split(",").map(Std.parseFloat);
|
||||
parsed = new Vec4( array[0], array[1], array[2], array[3] );
|
||||
runOutput(0);
|
||||
case "float":
|
||||
var flt:String = data.substr(api.length, data.length);
|
||||
var float = StringTools.trim(flt);
|
||||
parsed = Std.parseFloat(float);
|
||||
runOutput(0);
|
||||
case "integer":
|
||||
var int:String = data.substr(api.length, data.length);
|
||||
var integer = StringTools.trim(int);
|
||||
parsed = Std.parseInt(integer);
|
||||
runOutput(0);
|
||||
case "boolean":
|
||||
var boolean:String = data.substr(api.length, data.length);
|
||||
if(boolean == "true"){
|
||||
parsed = true;
|
||||
} else {
|
||||
parsed = false;
|
||||
}
|
||||
runOutput(0);
|
||||
case "transform":
|
||||
var tsfm = data.substr(api.length, data.length);
|
||||
tsfm = StringTools.replace(tsfm, "[", "");
|
||||
tsfm = StringTools.replace(tsfm, "]", "");
|
||||
tsfm = StringTools.replace(tsfm, " ", "");
|
||||
tsfm = StringTools.trim(tsfm);
|
||||
var array = tsfm.split(",").map(Std.parseFloat);
|
||||
parsed = new Mat4(
|
||||
array[0], array[1], array[2], array[3],
|
||||
array[4], array[5], array[6], array[7],
|
||||
array[8], array[9], array[10], array[11],
|
||||
array[12], array[13], array[14], array[15]
|
||||
);
|
||||
runOutput(0);
|
||||
case "rotation":
|
||||
var bytes = new haxe.io.BytesInput(buffer.readUntil(api));
|
||||
var rot = new Quat();
|
||||
for (f in Reflect.fields(rot)) {
|
||||
Reflect.setField(rot, f, bytes.readFloat());
|
||||
}
|
||||
parsed = rot;
|
||||
runOutput(0);
|
||||
default: throw "Failed to parse data.";
|
||||
}
|
||||
return;
|
||||
|
||||
|
||||
|
||||
switch (property0) {
|
||||
case "string":
|
||||
case "string":
|
||||
var bytes = buffer.readAllAvailableBytes();
|
||||
parsed = bytes.toString().substr(0, bytes.length - api.length);
|
||||
trace(parsed);
|
||||
runOutput(0);
|
||||
case "vector":
|
||||
var bytes = new haxe.io.BytesInput(buffer.readUntil(api));
|
||||
case "vector":
|
||||
var readBytes = buffer.readUntil(api);
|
||||
if (readBytes == null) return;
|
||||
var bytes = new haxe.io.BytesInput(readBytes);
|
||||
var vec = new Vec4();
|
||||
for (f in Reflect.fields(vec)) {
|
||||
Reflect.setField(vec, f, bytes.readFloat());
|
||||
}
|
||||
vec.x = bytes.readFloat();
|
||||
vec.y = bytes.readFloat();
|
||||
vec.z = bytes.readFloat();
|
||||
vec.w = bytes.readFloat();
|
||||
parsed = vec;
|
||||
runOutput(0);
|
||||
case "float":
|
||||
var bytes = new haxe.io.BytesInput(buffer.readUntil(api));
|
||||
case "float":
|
||||
var readBytes = buffer.readUntil(api);
|
||||
if (readBytes == null) return;
|
||||
var bytes = new haxe.io.BytesInput(readBytes);
|
||||
var float: Float = bytes.readFloat();
|
||||
parsed = float;
|
||||
runOutput(0);
|
||||
case "integer":
|
||||
var bytes = new haxe.io.BytesInput(buffer.readUntil(api));
|
||||
case "integer":
|
||||
var readBytes = buffer.readUntil(api);
|
||||
if (readBytes == null) return;
|
||||
var bytes = new haxe.io.BytesInput(readBytes);
|
||||
var integer: Int = bytes.readInt32();
|
||||
parsed = integer;
|
||||
runOutput(0);
|
||||
case "boolean":
|
||||
case "boolean":
|
||||
var bytes = buffer.readAllAvailableBytes();
|
||||
var boolean = bytes.toString().substr(0, bytes.length - api.length);
|
||||
if(boolean == "true"){
|
||||
if (boolean == "true") {
|
||||
parsed = true;
|
||||
} else {
|
||||
parsed = false;
|
||||
}
|
||||
runOutput(0);
|
||||
case "transform":
|
||||
var bytes = new haxe.io.BytesInput(buffer.readUntil(api));
|
||||
var readBytes = buffer.readUntil(api);
|
||||
if (readBytes == null) return;
|
||||
var bytes = new haxe.io.BytesInput(readBytes);
|
||||
var loc: Vec4 = new Vec4();
|
||||
var rot: Quat = new Quat();
|
||||
var scl: Vec4 = new Vec4();
|
||||
for (f in Reflect.fields(loc)) {
|
||||
Reflect.setField(loc, f, bytes.readFloat());
|
||||
try {
|
||||
loc.x = bytes.readFloat();
|
||||
loc.y = bytes.readFloat();
|
||||
loc.z = bytes.readFloat();
|
||||
loc.w = bytes.readFloat();
|
||||
rot.x = bytes.readFloat();
|
||||
rot.y = bytes.readFloat();
|
||||
rot.z = bytes.readFloat();
|
||||
rot.w = bytes.readFloat();
|
||||
scl.x = bytes.readFloat();
|
||||
scl.y = bytes.readFloat();
|
||||
scl.z = bytes.readFloat();
|
||||
scl.w = bytes.readFloat();
|
||||
} catch(e: Dynamic) {
|
||||
trace("readFloat error: " + e);
|
||||
return;
|
||||
}
|
||||
for (f in Reflect.fields(rot)) {
|
||||
Reflect.setField(rot, f, bytes.readFloat());
|
||||
}
|
||||
for (f in Reflect.fields(scl)) {
|
||||
Reflect.setField(scl, f, bytes.readFloat());
|
||||
}
|
||||
var transform = Mat4.identity();
|
||||
var transform = Mat4.identity();
|
||||
parsed = transform.compose(loc, rot, scl);
|
||||
runOutput(0);
|
||||
case "rotation":
|
||||
var bytes = new haxe.io.BytesInput(buffer.readUntil(api));
|
||||
case "rotation":
|
||||
var readBytes = buffer.readUntil(api);
|
||||
if (readBytes == null) return;
|
||||
var bytes = new haxe.io.BytesInput(readBytes);
|
||||
var rot = new Quat();
|
||||
for (f in Reflect.fields(rot)) {
|
||||
Reflect.setField(rot, f, bytes.readFloat());
|
||||
}
|
||||
rot.x = bytes.readFloat();
|
||||
rot.y = bytes.readFloat();
|
||||
rot.z = bytes.readFloat();
|
||||
rot.w = bytes.readFloat();
|
||||
parsed = rot;
|
||||
runOutput(0);
|
||||
default: throw "Failed to parse data.";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
return switch (from) {
|
||||
|
||||
@ -1,401 +1,124 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.network.Connect;
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.Buffer;
|
||||
import haxe.io.Bytes;
|
||||
import iron.object.Object;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.LeenkxClient;
|
||||
#end
|
||||
import iron.math.Vec4;
|
||||
import iron.math.Quat;
|
||||
import iron.math.Mat4;
|
||||
|
||||
|
||||
class LeenkxSendMessageNode extends LogicNode {
|
||||
public var property0: String;
|
||||
public var property1: String;
|
||||
public var id: String;
|
||||
public var all: Bool;
|
||||
public var property2: Bool;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if js
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
var api: String = inputs[2].get();
|
||||
if (connection == null) return;
|
||||
var api: String = inputs[2].get();
|
||||
var message: Dynamic = inputs[3].get();
|
||||
if (message == null) return;
|
||||
var broadcast: Bool = inputs.length > 4 && inputs[4].get() == true;
|
||||
var targetAddress: String = inputs.length > 5 ? inputs[5].get() : null;
|
||||
if (!broadcast && (targetAddress == null || targetAddress == "")) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if lnx_torrent
|
||||
var socket = Leenkx.connections[connection._url];
|
||||
if (socket == null) return;
|
||||
var lnxCn:LeenkxClient = socket.client;
|
||||
if (lnxCn == null) return;
|
||||
|
||||
var buffer = new Buffer();
|
||||
|
||||
switch (property1) {
|
||||
case "string":
|
||||
buffer.writeBytes(Bytes.ofString(message));
|
||||
case "vector":
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
var f:Null<Float>;
|
||||
if ((f = message.x) != null) bytesOut.writeFloat(f);
|
||||
if ((f = message.y) != null) bytesOut.writeFloat(f);
|
||||
if ((f = message.z) != null) bytesOut.writeFloat(f);
|
||||
if ((f = message.w) != null) bytesOut.writeFloat(f);
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
case "float":
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
bytesOut.writeFloat(message);
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
case "integer":
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
bytesOut.writeInt32(message);
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
case "boolean":
|
||||
if (message == true) {
|
||||
buffer.writeBytes(Bytes.ofString("true"));
|
||||
} else {
|
||||
buffer.writeBytes(Bytes.ofString("false"));
|
||||
}
|
||||
case "transform":
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
var transform:Mat4 = message;
|
||||
var loc = new Vec4();
|
||||
var rot = new Quat();
|
||||
var scale = new Vec4();
|
||||
transform.decompose(loc, rot, scale);
|
||||
bytesOut.writeFloat(loc.x);
|
||||
bytesOut.writeFloat(loc.y);
|
||||
bytesOut.writeFloat(loc.z);
|
||||
bytesOut.writeFloat(loc.w);
|
||||
bytesOut.writeFloat(rot.x);
|
||||
bytesOut.writeFloat(rot.y);
|
||||
bytesOut.writeFloat(rot.z);
|
||||
bytesOut.writeFloat(rot.w);
|
||||
bytesOut.writeFloat(scale.x);
|
||||
bytesOut.writeFloat(scale.y);
|
||||
bytesOut.writeFloat(scale.z);
|
||||
bytesOut.writeFloat(scale.w);
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
case "rotation":
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
var f:Null<Float>;
|
||||
if ((f = message.x) != null) bytesOut.writeFloat(f);
|
||||
if ((f = message.y) != null) bytesOut.writeFloat(f);
|
||||
if ((f = message.z) != null) bytesOut.writeFloat(f);
|
||||
if ((f = message.w) != null) bytesOut.writeFloat(f);
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
default: trace("Failed to send data.");
|
||||
}
|
||||
|
||||
if (api != null && api != "") {
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
|
||||
var rawBytes = buffer.readAllAvailableBytes();
|
||||
|
||||
try {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(`'+ api + message + '`);
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
if (property2) {
|
||||
var payload = haxe.crypto.Base64.encode(rawBytes);
|
||||
if (broadcast) {
|
||||
lnxCn.send(null, payload);
|
||||
} else {
|
||||
lnxCn.send(targetAddress, payload);
|
||||
}
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send(`' + inputs[4].get() + '`, `'+ api + message + '` );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
lnxCn.sendRawMessage(rawBytes, broadcast ? null : targetAddress);
|
||||
}
|
||||
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
return;
|
||||
runOutput(0);
|
||||
|
||||
switch (property1) {
|
||||
case "string":
|
||||
if(property0 == "client"){
|
||||
var buffer = new Buffer();
|
||||
buffer.writeBytes(Bytes.ofString(message));
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
|
||||
try {
|
||||
connection.buffer = buffer;
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'lnx_' + connection._url + '.send(`' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}else{
|
||||
var script = 'lnx_' + connection._url + '.send(`' + inputs[4].get() + '`, `' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
case "vector":
|
||||
if(property0 == "client"){
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
var i = Reflect.fields(message);
|
||||
for (b in i) {
|
||||
bytesOut.writeFloat(Reflect.field(message, b));
|
||||
}
|
||||
var buffer = new Buffer();
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
#else
|
||||
runOutput(0);
|
||||
|
||||
try {
|
||||
connection.buffer = buffer;
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send("' + api + message + '");
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
} else {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'lnx_' + connection._url + '.send(`' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}else{
|
||||
var script = 'lnx_' + connection._url + '.send(`' + inputs[4].get() + '`, `' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
case "float":
|
||||
if(property0 == "client"){
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
bytesOut.writeFloat(message);
|
||||
var buffer = new Buffer();
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
try {
|
||||
connection.buffer = buffer;
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
} else {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'lnx_' + connection._url + '.send(`' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}else{
|
||||
var script = 'lnx_' + connection._url + '.send(`' + inputs[4].get() + '`, `' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
case "integer":
|
||||
if(property0 == "client"){
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
bytesOut.writeInt32(message);
|
||||
var buffer = new Buffer();
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
try {
|
||||
connection.buffer = buffer;
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
} else {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'lnx_' + connection._url + '.send(`' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}else{
|
||||
var script = 'lnx_' + connection._url + '.send(`' + inputs[4].get() + '`, `' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
case "boolean":
|
||||
if(property0 == "client"){
|
||||
var buffer = new Buffer();
|
||||
if(message == true){
|
||||
buffer.writeBytes(Bytes.ofString("true"));
|
||||
} else {
|
||||
buffer.writeBytes(Bytes.ofString("false"));
|
||||
}
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
try {
|
||||
connection.buffer = buffer;
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
} else {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'lnx_' + connection._url + '.send(`' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}else{
|
||||
var script = 'lnx_' + connection._url + '.send(`' + inputs[4].get() + '`, `' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
case "transform":
|
||||
if(property0 == "client"){
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
var transform:Mat4 = message;
|
||||
var loc = new Vec4();
|
||||
var rot = new Quat();
|
||||
var scale = new Vec4();
|
||||
transform.decompose(loc, rot, scale);
|
||||
var l = Reflect.fields(loc);
|
||||
for (b in l) {
|
||||
bytesOut.writeFloat(Reflect.field(loc, b));
|
||||
}
|
||||
var r = Reflect.fields(rot);
|
||||
for (b in r) {
|
||||
bytesOut.writeFloat(Reflect.field(rot, b));
|
||||
}
|
||||
var s = Reflect.fields(scale);
|
||||
for (b in s) {
|
||||
bytesOut.writeFloat(Reflect.field(scale, b));
|
||||
}
|
||||
var buffer = new Buffer();
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
try {
|
||||
|
||||
connection.buffer = buffer;
|
||||
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
//console.log("Mine: " + lx_8001.torrent.discovery.peerId + " || Incomming: " + p);
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
//var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
var script = 'lnx_' + connection._url +'.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
}
|
||||
case "rotation":
|
||||
if(property0 == "client"){
|
||||
var bytesOut = new haxe.io.BytesOutput();
|
||||
var i = Reflect.fields(message);
|
||||
for (b in i) {
|
||||
bytesOut.writeFloat(Reflect.field(message, b));
|
||||
}
|
||||
var buffer = new Buffer();
|
||||
buffer.writeBytes(bytesOut.getBytes());
|
||||
if(api != "" || api != null){
|
||||
buffer.writeBytes(Bytes.ofString(api));
|
||||
}
|
||||
try {
|
||||
connection.buffer = buffer;
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send(leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'lnx_' + connection._url +'.send("' + inputs[4].get() + '",leenkx.network.Leenkx.connections.h["'+ connection._url +'"].buffer );';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
} else {
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'lnx_' + connection._url + '.send(`' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}else{
|
||||
var script = 'lnx_' + connection._url + '.send(`' + inputs[4].get() + '`, `' + api + message + '`);';
|
||||
js.Syntax.code('(1, eval)({0})', script);
|
||||
}
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*******_________________________________________
|
||||
|
||||
try {
|
||||
|
||||
if(inputs[5].get() == true){
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
lnx_' + connection._url +'.torrent._peers[p].conn.send("' + api + message + '");
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
} else {
|
||||
var script = 'for (p in lnx_' + connection._url +'.torrent._peers){
|
||||
try{
|
||||
|
||||
lx_' + connection._url +'.torrent._peers[' + inputs[4].get() + '].conn.send("' + api + message + '");
|
||||
}catch(error){
|
||||
console.log("Error: " + error);
|
||||
}
|
||||
}';
|
||||
js.Syntax.code('(1, eval)({0})', script.toString());
|
||||
runOutput(0);
|
||||
}
|
||||
} catch(error) {
|
||||
trace("Error: " + error);
|
||||
}
|
||||
return;
|
||||
|
||||
_________________________________________*****/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ class NetworkMessageParserNode extends LogicNode {
|
||||
|
||||
var buffer = cast(data, Buffer);
|
||||
api = inputs[1].get();
|
||||
if (api == null) api = "";
|
||||
if(buffer.endsWith(api) == false){
|
||||
return;
|
||||
}
|
||||
|
||||
@ -7,6 +7,9 @@ class OnActionMarkerNode extends LogicNode {
|
||||
public var property0: String;
|
||||
public var property1: String;
|
||||
|
||||
var registeredSamplers: Array<iron.object.Animation.ActionSampler> = [];
|
||||
var registeredCallbacks: Map<iron.object.Animation.ActionSampler, Void->Void> = new Map();
|
||||
|
||||
public function new(tree: LogicTree) {
|
||||
super(tree);
|
||||
|
||||
@ -21,9 +24,33 @@ class OnActionMarkerNode extends LogicNode {
|
||||
assert(Error, object != null, "Object input cannot be null");
|
||||
var animation = object.animation;
|
||||
if (animation == null) animation = object.getBoneAnimation(object.uid);
|
||||
var action = animation.activeActions.get(actionID);
|
||||
if(action == null) return;
|
||||
animation.notifyOnMarker(action, marker, function() { runOutput(0); });
|
||||
tree.removeUpdate(init);
|
||||
if (animation == null) return;
|
||||
var active = animation.activeActions;
|
||||
if (active == null) return;
|
||||
|
||||
var i = registeredSamplers.length;
|
||||
while (i-- > 0) {
|
||||
var alive = false;
|
||||
for (sampler in active) if (sampler == registeredSamplers[i]) {
|
||||
alive = true;
|
||||
break;
|
||||
}
|
||||
if (!alive) {
|
||||
var s = registeredSamplers[i];
|
||||
animation.removeMarker(s, marker, registeredCallbacks.get(s));
|
||||
registeredCallbacks.remove(s);
|
||||
registeredSamplers.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
for (key in active.keys()) {
|
||||
var sampler = active.get(key);
|
||||
if (actionID != "" && key != actionID && sampler.action != actionID) continue;
|
||||
if (registeredSamplers.indexOf(sampler) != -1) continue;
|
||||
registeredSamplers.push(sampler);
|
||||
var cb = function() { runOutput(0); };
|
||||
registeredCallbacks.set(sampler, cb);
|
||||
animation.notifyOnMarker(sampler, marker, cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
65
leenkx/Sources/leenkx/logicnode/OnTorrentEventNode.hx
Normal file
65
leenkx/Sources/leenkx/logicnode/OnTorrentEventNode.hx
Normal file
@ -0,0 +1,65 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
#end
|
||||
|
||||
class OnTorrentEventNode extends LogicNode {
|
||||
public var property0:String;
|
||||
public var eventValue:Dynamic;
|
||||
var net_Url:String;
|
||||
var torrent:Dynamic;
|
||||
var registeredTorrents:Map<String, Bool> = new Map();
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
var id = inputs[2].get();
|
||||
if (id == null) return;
|
||||
net_Url = connection._url;
|
||||
|
||||
var socket = Leenkx.connections[connection._url];
|
||||
var client = socket != null ? socket.client : null;
|
||||
var t = client != null ? client.get(id) : (Std.isOfType(id, Torrent) ? id : null);
|
||||
if (t == null) return;
|
||||
|
||||
var event = property0 != null ? property0 : "done";
|
||||
var self = this;
|
||||
var fire = function(v:Dynamic) {
|
||||
self.eventValue = v;
|
||||
self.runOutput(0);
|
||||
};
|
||||
var key = t.infoHashHex != null ? t.infoHashHex : "v2:" + t.infoHashV2Hex;
|
||||
var alreadyRegistered = registeredTorrents.exists(key);
|
||||
if (!alreadyRegistered) registeredTorrents.set(key, true);
|
||||
switch (event) {
|
||||
case "ready":
|
||||
if (t.ready) fire(null);
|
||||
else if (!alreadyRegistered) t.on("ready", function() fire(null));
|
||||
case "piece" | "wire" | "metadata" | "error":
|
||||
if (!alreadyRegistered) t.on(event, function(v) fire(v));
|
||||
default:
|
||||
if (t.done) fire(null);
|
||||
else if (!alreadyRegistered) t.on("done", function() fire(null));
|
||||
}
|
||||
torrent = t;
|
||||
#end
|
||||
}
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: torrent;
|
||||
case 2: eventValue;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
73
leenkx/Sources/leenkx/logicnode/ParseTorrentNode.hx
Normal file
73
leenkx/Sources/leenkx/logicnode/ParseTorrentNode.hx
Normal file
@ -0,0 +1,73 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.Magnet;
|
||||
import leenkx.network.torrent.MetaInfo;
|
||||
#end
|
||||
|
||||
class ParseTorrentNode extends LogicNode {
|
||||
var meta:Dynamic;
|
||||
var magnet:Dynamic;
|
||||
var idHex:String;
|
||||
public var property0:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
meta = null;
|
||||
magnet = null;
|
||||
idHex = null;
|
||||
var src = inputs[1].get();
|
||||
if (src == null) return;
|
||||
|
||||
switch (property0 != null ? property0 : "magnet") {
|
||||
case "file":
|
||||
if (Std.isOfType(src, MetaInfo)) {
|
||||
meta = src;
|
||||
} else {
|
||||
var b = Std.isOfType(src, Bytes) ? src : Bytes.ofString(Std.string(src));
|
||||
try {
|
||||
meta = MetaInfo.fromTorrentBytes(b);
|
||||
} catch(e:Dynamic) {
|
||||
trace(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
case "id":
|
||||
var s = Std.string(src);
|
||||
idHex = s.length == 64 ? "v2:" + s : s;
|
||||
default:
|
||||
var m = Magnet.parse(Std.string(src));
|
||||
if (m == null) {
|
||||
return;
|
||||
}
|
||||
magnet = m;
|
||||
idHex = m.infoHashHex != null ? m.infoHashHex : m.infoHashV2Hex;
|
||||
}
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
var m:MetaInfo = meta;
|
||||
var mg:Magnet = magnet;
|
||||
return switch (from) {
|
||||
case 1: m;
|
||||
case 2: m != null ? (m.infoHashHex != null ? m.infoHashHex : m.infoHashV2Hex) : idHex;
|
||||
case 3: m != null ? m.name : (mg != null ? mg.name : null);
|
||||
case 4: m != null ? m.announce : (mg != null ? mg.trackers : null);
|
||||
case 5: m != null ? m.urlList : (mg != null ? mg.webSeeds : null);
|
||||
case 6: m != null ? m.files : null;
|
||||
case 7: m != null ? m.totalLength : 0;
|
||||
case 8: m != null ? m.toTorrentBytes() : null;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
@ -27,11 +27,11 @@ class PlayAnimationTreeNode extends LogicNode {
|
||||
}
|
||||
|
||||
function playAnim() {
|
||||
if (init = false) return;
|
||||
if (init == false) return;
|
||||
|
||||
init = false;
|
||||
tree.removeUpdate(playAnim);
|
||||
|
||||
|
||||
var animation = object.animation;
|
||||
if(animation == null) {
|
||||
#if lnx_skin
|
||||
|
||||
@ -16,8 +16,10 @@ class RestartActionNode extends LogicNode {
|
||||
var animation = object.animation;
|
||||
|
||||
if (animation == null) animation = object.getBoneAnimation(object.uid);
|
||||
if(animation.activeActions == null) return;
|
||||
animation.activeActions.get(action).restartAction();
|
||||
if (animation == null || animation.activeActions == null) return;
|
||||
var sampler = animation.activeActions.get(action);
|
||||
if (sampler == null) return;
|
||||
sampler.restartAction();
|
||||
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
@ -1,52 +1,124 @@
|
||||
package leenkx.logicnode;
|
||||
import leenkx.system.Event;
|
||||
import iron.object.Object;
|
||||
|
||||
#if lnx_torrent
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.MetaInfo;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
#end
|
||||
|
||||
|
||||
class SeedTorrentNode extends LogicNode {
|
||||
public var torrent: Dynamic;
|
||||
public var net_Url: Dynamic;
|
||||
|
||||
public var torrent:Dynamic;
|
||||
public var net_Url:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if js
|
||||
#if lnx_torrent
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
trace("got connection");
|
||||
var data = inputs[3].get();
|
||||
if (data == null) return;
|
||||
trace("got data");
|
||||
var opts = inputs[2].get();
|
||||
if (connection == null) return;
|
||||
var src = inputs[2].get();
|
||||
net_Url = connection._url;
|
||||
|
||||
var cx = "lx_" + net_Url;
|
||||
var lx:Dynamic = untyped js.Browser.window[cx];
|
||||
var args = opts.toString();
|
||||
trace(args);
|
||||
var func = Reflect.field(lx.wt, "seed");
|
||||
if (func != null) {
|
||||
torrent = func(data, function (torrent) {
|
||||
trace('Client is seeding ' + torrent.magnetURI);
|
||||
});
|
||||
//trace(torrent);
|
||||
var socket = Leenkx.connections[connection._url];
|
||||
if (socket == null || socket.client == null) return;
|
||||
var client = socket.client;
|
||||
var self = this;
|
||||
|
||||
var onSeeded = function(t:Torrent) {
|
||||
if (t == null) return;
|
||||
self.torrent = t;
|
||||
var onDone = function() {
|
||||
self.runOutput(2);
|
||||
};
|
||||
if (t.done) onDone();
|
||||
else t.on("done", onDone);
|
||||
};
|
||||
|
||||
if (Std.isOfType(src, Torrent)) {
|
||||
var t:Torrent = src;
|
||||
t.start();
|
||||
torrent = t;
|
||||
runOutput(0);
|
||||
return;
|
||||
}
|
||||
|
||||
var meta:MetaInfo = null;
|
||||
if (Std.isOfType(src, MetaInfo)) {
|
||||
meta = cast src;
|
||||
} else if (Std.isOfType(src, Bytes)) {
|
||||
try {
|
||||
meta = MetaInfo.fromTorrentBytes(cast src);
|
||||
} catch(e:Dynamic) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
var files = filesFor(meta, inputs.length > 3 ? inputs[3].get() : null);
|
||||
if (files == null) {
|
||||
return;
|
||||
}
|
||||
client.seedMeta(meta, files, null, onSeeded);
|
||||
runOutput(0);
|
||||
#end
|
||||
}
|
||||
|
||||
override function get(from: Int): Dynamic {
|
||||
return switch (from) {
|
||||
case 1: torrent;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
}
|
||||
#if lnx_torrent
|
||||
function filesFor(meta:MetaInfo, data:Dynamic):Array<{path:String, data:Bytes}> {
|
||||
if (data == null) return null;
|
||||
var entries = meta.files;
|
||||
if (Std.isOfType(data, Bytes)) {
|
||||
var blob:Bytes = data;
|
||||
if (blob.length != meta.totalLength) return null;
|
||||
var files:Array<{path:String, data:Bytes}> = [];
|
||||
var cursor = 0;
|
||||
for (f in entries) {
|
||||
files.push({path: f.path, data: blob.sub(cursor, f.length)});
|
||||
cursor += f.length;
|
||||
}
|
||||
return files;
|
||||
}
|
||||
if (Std.isOfType(data, Array)) {
|
||||
var items:Array<Dynamic> = data;
|
||||
var files:Array<{path:String, data:Bytes}> = [];
|
||||
for (it in items) {
|
||||
var p = Reflect.field(it, "path");
|
||||
var d:Bytes = Reflect.field(it, "data");
|
||||
if (d == null) d = Reflect.field(it, "content");
|
||||
if (p == null || d == null) continue;
|
||||
var sp = Std.string(p);
|
||||
var matched = false;
|
||||
for (f in entries) {
|
||||
if (f.path == sp || StringTools.endsWith(f.path, "/" + sp)) {
|
||||
files.push({path: f.path, data: d});
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!matched) files.push({path: sp, data: d});
|
||||
}
|
||||
if (files.length == entries.length) return files;
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
#end
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
var t:Torrent = torrent;
|
||||
return switch (from) {
|
||||
case 1: torrent;
|
||||
case 3: t != null ? t.infoHashHex : null;
|
||||
case 4: t != null ? t.magnetURI : null;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -19,8 +19,10 @@ class SetActionPausedNode extends LogicNode {
|
||||
var animation = object.animation;
|
||||
|
||||
if (animation == null) animation = object.getBoneAnimation(object.uid);
|
||||
if(animation.activeActions == null) return;
|
||||
animation.activeActions.get(action).paused = paused;
|
||||
if (animation == null || animation.activeActions == null) return;
|
||||
var sampler = animation.activeActions.get(action);
|
||||
if (sampler == null) return;
|
||||
sampler.paused = paused;
|
||||
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
90
leenkx/Sources/leenkx/logicnode/SetTrackersNode.hx
Normal file
90
leenkx/Sources/leenkx/logicnode/SetTrackersNode.hx
Normal file
@ -0,0 +1,90 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
import leenkx.network.torrent.TorrentClient;
|
||||
#end
|
||||
|
||||
class SetTrackersNode extends LogicNode {
|
||||
public var property0:String;
|
||||
public var target:Dynamic;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
var p = property0 != null ? property0 : "add";
|
||||
switch (p) {
|
||||
case "add" | "override":
|
||||
setTorrentTrackers(p == "override");
|
||||
default:
|
||||
setTrackerProperty(p);
|
||||
}
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
#if lnx_torrent
|
||||
function setTorrentTrackers(trackerOverride:Bool):Void {
|
||||
var id = inputs[2].get();
|
||||
var urls = TorrentClient.trackerUrls(inputs[3].get());
|
||||
if (urls == null) return;
|
||||
var t:Torrent = Std.isOfType(id, Torrent) ? id : null;
|
||||
if (t == null && id != null) {
|
||||
var connection = inputs[1].get();
|
||||
var socket = connection != null ? Leenkx.connections[connection._url] : null;
|
||||
var client = socket != null ? socket.client : null;
|
||||
if (client != null) t = client.get(id);
|
||||
}
|
||||
if (t != null) {
|
||||
target = t;
|
||||
if (trackerOverride) {
|
||||
t.setTrackers(urls);
|
||||
} else {
|
||||
for (u in urls) t.addTracker(u);
|
||||
}
|
||||
} else if (id == null) {
|
||||
target = null;
|
||||
if (trackerOverride) {
|
||||
TorrentClient.defaultTrackers = urls;
|
||||
} else {
|
||||
TorrentClient.mergeTrackers(TorrentClient.defaultTrackers, urls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setTrackerProperty(prop:String):Void {
|
||||
var tracker:Dynamic = inputs[1].get();
|
||||
if (tracker == null) return;
|
||||
var v = inputs[2].get();
|
||||
if (v == null) return;
|
||||
var field = prop == "interval" ? "intervalSec" : prop;
|
||||
var val:Dynamic = field == "trackerId" ? Std.string(v)
|
||||
: Std.int(Std.parseFloat(Std.string(v)));
|
||||
var setter:Dynamic = field == "intervalSec"
|
||||
&& Reflect.field(tracker, "intervalSec") == null
|
||||
? Reflect.field(tracker, "setAnnounceInterval") : null;
|
||||
if (setter != null) {
|
||||
try {
|
||||
Reflect.callMethod(tracker, setter, [(val:Int) * 1000]);
|
||||
} catch(e:Dynamic) {}
|
||||
} else {
|
||||
Reflect.setField(tracker, field, val);
|
||||
}
|
||||
target = tracker;
|
||||
}
|
||||
#end
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: target;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
74
leenkx/Sources/leenkx/logicnode/TorrentClientInfoNode.hx
Normal file
74
leenkx/Sources/leenkx/logicnode/TorrentClientInfoNode.hx
Normal file
@ -0,0 +1,74 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
#end
|
||||
|
||||
class TorrentClientInfoNode extends LogicNode {
|
||||
var stats:Dynamic;
|
||||
var list:Array<Dynamic>;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
list = [];
|
||||
stats = {downRate: 0.0, upRate: 0.0, progress: 0.0, ratio: 0.0};
|
||||
var connection = inputs[1].get();
|
||||
if (connection != null) {
|
||||
var socket = Leenkx.connections[connection._url];
|
||||
var client = socket != null ? socket.client : null;
|
||||
if (client != null) collect(client);
|
||||
}
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
#if lnx_torrent
|
||||
function collect(client:Dynamic) {
|
||||
var down = 0.0;
|
||||
var up = 0.0;
|
||||
var received = 0.0;
|
||||
var sent = 0.0;
|
||||
var total = 0.0;
|
||||
var left = 0.0;
|
||||
var torrents:Map<String, Torrent> = client.torrents;
|
||||
for (t in torrents) {
|
||||
list.push(t);
|
||||
var s = t.getStats();
|
||||
down += s.downRate;
|
||||
up += s.upRate;
|
||||
received += s.downloaded;
|
||||
sent += s.uploaded;
|
||||
if (t.meta != null) {
|
||||
total += t.meta.totalLength;
|
||||
left += s.left;
|
||||
}
|
||||
}
|
||||
stats = {
|
||||
downRate: down,
|
||||
upRate: up,
|
||||
progress: total > 0 ? 1.0 - left / total : 0.0,
|
||||
ratio: received > 0 ? sent / received : 0.0
|
||||
};
|
||||
}
|
||||
#end
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
return switch (from) {
|
||||
case 1: list;
|
||||
case 2: list.length;
|
||||
case 3: stats.downRate;
|
||||
case 4: stats.upRate;
|
||||
case 5: stats.progress;
|
||||
case 6: stats.ratio;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
}
|
||||
93
leenkx/Sources/leenkx/logicnode/TorrentControlNode.hx
Normal file
93
leenkx/Sources/leenkx/logicnode/TorrentControlNode.hx
Normal file
@ -0,0 +1,93 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
#end
|
||||
|
||||
class TorrentControlNode extends LogicNode {
|
||||
public var property0:String;
|
||||
var net_Url:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
var id = inputs[2].get();
|
||||
if (id == null) return;
|
||||
net_Url = connection._url;
|
||||
|
||||
var socket = Leenkx.connections[connection._url];
|
||||
if (socket == null) return;
|
||||
var client = socket.client;
|
||||
var t = client != null ? client.get(id) : (Std.isOfType(id, Torrent) ? id : null);
|
||||
var action = property0 != null ? property0 : "remove";
|
||||
|
||||
var file:Dynamic = inputs.length > 3 ? inputs[3].get() : null;
|
||||
var t2 = t;
|
||||
var startB = 0;
|
||||
var endB = -1;
|
||||
if (file != null) {
|
||||
startB = file.offset;
|
||||
endB = file.offset + file.length - 1;
|
||||
if (t2 == null) t2 = file.torrent;
|
||||
} else {
|
||||
var sv = inputs.length > 4 ? inputs[4].get() : null;
|
||||
var ev = inputs.length > 5 ? inputs[5].get() : null;
|
||||
if (sv != null && Std.string(sv) != "") {
|
||||
startB = Std.int(Std.parseFloat(Std.string(sv)));
|
||||
}
|
||||
if (ev != null && Std.string(ev) != "") {
|
||||
endB = Std.int(Std.parseFloat(Std.string(ev)));
|
||||
}
|
||||
}
|
||||
var pv = inputs.length > 6 ? inputs[6].get() : null;
|
||||
var priority = pv != null && Std.string(pv) != "" ? Std.int(Std.parseFloat(Std.string(pv))) : null;
|
||||
|
||||
switch (action) {
|
||||
case "start":
|
||||
if (t != null) t.start();
|
||||
case "stop":
|
||||
if (t != null) t.stop();
|
||||
case "select", "critical", "deselect":
|
||||
if (t2 != null) {
|
||||
if (endB < 0) {
|
||||
endB = t2.meta != null ? t2.meta.totalLength - 1 : 0;
|
||||
}
|
||||
if (action == "critical") {
|
||||
t2.critical(startB, endB);
|
||||
} else if (action == "select") {
|
||||
t2.select(startB, endB, priority);
|
||||
} else {
|
||||
t2.deselect(startB, endB, priority);
|
||||
}
|
||||
}
|
||||
case "add-peer":
|
||||
var peer = inputs.length > 3 ? inputs[3].get() : null;
|
||||
if (t != null && peer != null) {
|
||||
t.addPeer(Std.string(peer));
|
||||
}
|
||||
case "add-web-seed":
|
||||
var url = inputs.length > 3 ? inputs[3].get() : null;
|
||||
if (t != null && url != null) {
|
||||
t.addWebSeed(Std.string(url));
|
||||
}
|
||||
case "remove", "remove-data":
|
||||
var hex = t != null ? t.infoHashHex : Std.string(id);
|
||||
if (hex == null) hex = Std.string(id);
|
||||
if (client != null) {
|
||||
client.remove(hex, action == "remove-data");
|
||||
}
|
||||
if (t != null && Leenkx.torrent.get(net_Url) == t) {
|
||||
Leenkx.torrent.remove(net_Url);
|
||||
Leenkx.file.remove(net_Url);
|
||||
}
|
||||
default:
|
||||
}
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
}
|
||||
74
leenkx/Sources/leenkx/logicnode/TorrentInfoNode.hx
Normal file
74
leenkx/Sources/leenkx/logicnode/TorrentInfoNode.hx
Normal file
@ -0,0 +1,74 @@
|
||||
package leenkx.logicnode;
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Leenkx;
|
||||
import leenkx.network.torrent.Torrent;
|
||||
import leenkx.network.torrent.TorrentClient;
|
||||
#end
|
||||
|
||||
class TorrentInfoNode extends LogicNode {
|
||||
public var torrent:Dynamic;
|
||||
public var net_Url:String;
|
||||
|
||||
public function new(tree:LogicTree) {
|
||||
super(tree);
|
||||
}
|
||||
|
||||
override function run(from:Int) {
|
||||
#if lnx_torrent
|
||||
var id = inputs[2].get();
|
||||
if (id == null) return;
|
||||
torrent = null;
|
||||
|
||||
if (Std.isOfType(id, Torrent)) {
|
||||
torrent = id;
|
||||
} else {
|
||||
var connection = inputs[1].get();
|
||||
if (connection == null) return;
|
||||
net_Url = connection._url;
|
||||
var socket = Leenkx.connections[net_Url];
|
||||
if (socket == null || socket.client == null) return;
|
||||
torrent = socket.client.get(id);
|
||||
}
|
||||
#end
|
||||
runOutput(0);
|
||||
}
|
||||
|
||||
override function get(from:Int):Dynamic {
|
||||
#if lnx_torrent
|
||||
var t:Torrent = torrent;
|
||||
return switch (from) {
|
||||
case 1: t != null ? t.infoHashHex : null;
|
||||
case 2: t != null && t.meta != null ? t.meta.name : null;
|
||||
case 3: t != null ? t.numPeers : 0;
|
||||
case 4: progress(t);
|
||||
case 5: t != null ? t.getStats().downloaded : 0.0;
|
||||
case 6: t != null ? t.getStats().uploaded : 0.0;
|
||||
case 7: t != null ? t.done : false;
|
||||
case 8: t != null ? t.magnetURI : null;
|
||||
case 9: t != null ? t.files : null;
|
||||
case 10: t != null && t.meta != null ? t.meta.numPieces : 0;
|
||||
case 11: t != null && t.meta != null ? t.meta.pieceLength : 0;
|
||||
case 12: t != null && t.meta != null ? t.meta.totalLength : 0;
|
||||
case 13: t != null && t.meta != null ? t.meta.comment : null;
|
||||
case 14: t != null && t.meta != null ? t.meta.createdBy : null;
|
||||
case 15: t != null && t.meta != null ? t.meta.protocolVersion : 1;
|
||||
case 16: t != null ? t.infoHashV2Hex : null;
|
||||
case 17: t != null ? t.getStats().downRate : 0.0;
|
||||
case 18: t != null ? t.getStats().upRate : 0.0;
|
||||
case 19: t != null ? t.stopped : false;
|
||||
case 20: t;
|
||||
default: throw "Unreachable";
|
||||
}
|
||||
#else
|
||||
return null;
|
||||
#end
|
||||
}
|
||||
|
||||
#if lnx_torrent
|
||||
function progress(t:Torrent):Float {
|
||||
if (t == null || t.meta == null || t.meta.totalLength == 0) return 0;
|
||||
var stats = t.getStats();
|
||||
return 1.0 - stats.left / t.meta.totalLength;
|
||||
}
|
||||
#end
|
||||
}
|
||||
@ -63,10 +63,10 @@ class Buffer {
|
||||
public function readUntil(delimiter:String):Bytes {
|
||||
var dl = delimiter.length;
|
||||
|
||||
for (i in 0 ... available - dl) {
|
||||
for (i in 0 ... available - dl + 1) {
|
||||
var matched = true;
|
||||
for (j in 0 ... dl) {
|
||||
if (peekByte(currentOffset + i + j + 1) == delimiter.charCodeAt(j)) {
|
||||
if (peekByte(i + j) == delimiter.charCodeAt(j)) {
|
||||
continue;
|
||||
}
|
||||
matched = false;
|
||||
@ -74,7 +74,8 @@ class Buffer {
|
||||
}
|
||||
|
||||
if (matched) {
|
||||
var bytes = readBytes(i + dl + 1);
|
||||
var bytes = readBytes(i);
|
||||
readBytes(dl);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
@ -85,7 +86,19 @@ class Buffer {
|
||||
public function readBytes(count:Int):Bytes {
|
||||
var count2 = Std.int(Math.min(count, available));
|
||||
var out = Bytes.alloc(count2);
|
||||
for (n in 0 ... count2) out.set(n, readByte());
|
||||
var written = 0;
|
||||
while (written < count2) {
|
||||
if (currentData == null || currentOffset >= currentData.length) {
|
||||
currentOffset = 0;
|
||||
currentData = chunks.shift();
|
||||
}
|
||||
var n = Std.int(Math.min(count2 - written, currentData.length - currentOffset));
|
||||
out.blit(written, currentData, currentOffset, n);
|
||||
written += n;
|
||||
currentOffset += n;
|
||||
available -= n;
|
||||
}
|
||||
length = available;
|
||||
return out;
|
||||
}
|
||||
|
||||
@ -115,16 +128,23 @@ class Buffer {
|
||||
}
|
||||
|
||||
public function peekByte(offset:Int):Int {
|
||||
if (available <= 0) throw 'No bytes available';
|
||||
var tempOffset = offset;
|
||||
var tempData = chunks[0];
|
||||
if (tempData == null) {
|
||||
tempData = currentData;
|
||||
if (available <= 0 || offset < 0 || offset >= available) throw 'No bytes available';
|
||||
var tempData = currentData;
|
||||
var chunkIndex = -1;
|
||||
var tempOffset:Int;
|
||||
if (tempData != null && currentOffset < tempData.length) {
|
||||
tempOffset = currentOffset + offset;
|
||||
} else {
|
||||
tempData = chunks[0];
|
||||
chunkIndex = 0;
|
||||
tempOffset = offset;
|
||||
}
|
||||
var chunkIndex = 0;
|
||||
while (tempOffset >= tempData.length) {
|
||||
tempOffset -= tempData.length;
|
||||
chunkIndex++;
|
||||
if (chunkIndex >= chunks.length){
|
||||
trace('No bytes available');
|
||||
}
|
||||
tempData = chunks[chunkIndex];
|
||||
}
|
||||
return tempData.get(tempOffset);
|
||||
@ -154,9 +174,9 @@ class Buffer {
|
||||
|
||||
public function endsWith(e:String):Bool {
|
||||
var i = available - e.length;
|
||||
var n = currentOffset;
|
||||
var n = 0;
|
||||
|
||||
if (i <= 0) {
|
||||
if (i < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
package leenkx.network;
|
||||
|
||||
class Handler extends WebSocketCommon {
|
||||
public var validateHandshake:(HttpRequest, HttpResponse, (HttpResponse) -> Void) -> Void = null;
|
||||
|
||||
public function new(socket:SocketImpl) {
|
||||
super(socket);
|
||||
isClient = false;
|
||||
|
||||
@ -4,6 +4,7 @@ class HttpHeader {
|
||||
public static inline var SEC_WEBSOCKET_KEY:String = "Sec-WebSocket-Key";
|
||||
public static inline var SEC_WEBSOSCKET_ACCEPT:String = "Sec-WebSocket-Accept";
|
||||
public static inline var SEC_WEBSOSCKET_VERSION:String = "Sec-WebSocket-Version";
|
||||
public static inline var SEC_WEBSOCKET_PROTOCOL:String = "Sec-WebSocket-Protocol";
|
||||
public static inline var UPGRADE:String = "Upgrade";
|
||||
public static inline var HOST:String = "Host";
|
||||
public static inline var CONNECTION:String = "Connection";
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package leenkx.network;
|
||||
|
||||
#if lnx_torrent
|
||||
import leenkx.network.Types;
|
||||
import haxe.io.Bytes;
|
||||
import iron.object.Object;
|
||||
import leenkx.system.Event;
|
||||
import leenkx.network.Buffer;
|
||||
import leenkx.network.torrent.LeenkxClient;
|
||||
|
||||
@:expose
|
||||
class Leenkx {
|
||||
@ -20,24 +22,20 @@ class Leenkx {
|
||||
public static var onPingEvent: String = "Leenkx.onPing";
|
||||
public static var onLeftEvent: String = "Leenkx.onLeft";
|
||||
public static var onTimeOutEvent: String = "Leenkx.onTimeOut";
|
||||
public static var onRpcEvent: String = "Leenkx.onRpc";
|
||||
public static var onRpcResponseEvent: String = "Leenkx.onRpcResponse";
|
||||
public static var onWireLeftEvent: String = "Leenkx.onWireLeft";
|
||||
public static var onWireSeenEvent: String = "Leenkx.onWireSeen";
|
||||
public static var onTorrentEvent: String = "Leenkx.onTorrent";
|
||||
public static var onTorrentAddedEvent: String = "Leenkx.onTorrentAdded";
|
||||
public static var onTrackerEvent: String = "Leenkx.onTracker";
|
||||
public static var onAnnounceEvent: String = "Leenkx.onAnnounce";
|
||||
public static var onTorrentDoneEvent: String = "Leenkx.onTorrentDone";
|
||||
public static var connections:Map<String, leenkx.network.LeenkxSocket> = [];
|
||||
#if js
|
||||
public static var peers:js.lib.Map<String, String> = new js.lib.Map<String,String>();
|
||||
public static var data:js.lib.Map<String, Dynamic> = new js.lib.Map<String,Dynamic>();
|
||||
public static var id:js.lib.Map<String, String> = new js.lib.Map<String,String>();
|
||||
public static var torrent:js.lib.Map<String, Dynamic> = new js.lib.Map<String,Dynamic>();
|
||||
public static var file:js.lib.Map<String, Dynamic> = new js.lib.Map<String,Dynamic>();
|
||||
#end
|
||||
public static var lxNew:Void->Void;
|
||||
|
||||
public static var peers:Map<String, String> = [];
|
||||
public static var data:Map<String, Dynamic> = [];
|
||||
public static var id:Map<String, String> = [];
|
||||
public static var torrent:Map<String, Dynamic> = [];
|
||||
public static var file:Map<String, Dynamic> = [];
|
||||
|
||||
public function new(net_Url: String, net_object: Object) {
|
||||
if (net_Url != null && net_object != null) {
|
||||
|
||||
@ -54,224 +52,65 @@ class Leenkx {
|
||||
}
|
||||
|
||||
if (object != null) {
|
||||
final loadEvent = Event.get(Leenkx.onLoadEvent);
|
||||
final openEvent = Event.get(Leenkx.onOpenEvent);
|
||||
final messageEvent = Event.get(Leenkx.onMessageEvent);
|
||||
final errorEvent = Event.get(Leenkx.onErrorEvent);
|
||||
final closeEvent = Event.get(Leenkx.onCloseEvent);
|
||||
final seenEvent = Event.get(Leenkx.onSeenEvent);
|
||||
final serverEvent = Event.get(Leenkx.onServerEvent);
|
||||
final connectionsEvent = Event.get(Leenkx.onConnectionsEvent);
|
||||
final pingEvent = Event.get(Leenkx.onPingEvent);
|
||||
final leftEvent = Event.get(Leenkx.onLeftEvent);
|
||||
final timeoutEvent = Event.get(Leenkx.onTimeOutEvent);
|
||||
final rpcEvent = Event.get(Leenkx.onRpcEvent);
|
||||
final rpcresponseEvent = Event.get(Leenkx.onRpcResponseEvent);
|
||||
final wireleftEvent = Event.get(Leenkx.onWireLeftEvent);
|
||||
final wireseenEvent = Event.get(Leenkx.onWireSeenEvent);
|
||||
final torrentEvent = Event.get(Leenkx.onTorrentEvent);
|
||||
final trackerEvent = Event.get(Leenkx.onTrackerEvent);
|
||||
final announceEvent = Event.get(Leenkx.onAnnounceEvent);
|
||||
final torrentDoneEvent = Event.get(Leenkx.onTorrentDoneEvent);
|
||||
var uid = object.uid;
|
||||
|
||||
Leenkx.connections[net_Url].onopen = function() {
|
||||
if (openEvent != null) {
|
||||
for (e in openEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onOpenEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onmessage = function() {
|
||||
if (messageEvent != null) {
|
||||
for (e in messageEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onMessageEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onerror = function() {
|
||||
if (errorEvent != null) {
|
||||
for (e in errorEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onErrorEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onclose = function() {
|
||||
if (closeEvent != null) {
|
||||
for (e in closeEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onCloseEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onseen = function() {
|
||||
if (seenEvent != null) {
|
||||
for (e in seenEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onSeenEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onserver = function() {
|
||||
if (serverEvent != null) {
|
||||
for (e in serverEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onServerEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onconnections = function() {
|
||||
if (connectionsEvent != null) {
|
||||
for (e in connectionsEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onConnectionsEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onping = function() {
|
||||
if (pingEvent != null) {
|
||||
for (e in pingEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onPingEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onleft = function() {
|
||||
if (leftEvent != null) {
|
||||
for (e in leftEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onLeftEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].ontimeout = function() {
|
||||
if (timeoutEvent != null) {
|
||||
for (e in timeoutEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Leenkx.connections[net_Url].onrpc = function() {
|
||||
if (rpcEvent != null) {
|
||||
for (e in rpcEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Leenkx.connections[net_Url].onrpcresponse = function() {
|
||||
if (rpcresponseEvent != null) {
|
||||
for (e in rpcresponseEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onTimeOutEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onwireleft = function() {
|
||||
if (wireleftEvent != null) {
|
||||
for (e in wireleftEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onWireLeftEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onwireseen = function() {
|
||||
if (wireseenEvent != null) {
|
||||
for (e in wireseenEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onWireSeenEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].ontorrent = function() {
|
||||
if (torrentEvent != null) {
|
||||
for (e in torrentEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Event.send(Leenkx.onTorrentEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].ontorrentadded = function() {
|
||||
Event.send(Leenkx.onTorrentAddedEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].ontracker = function() {
|
||||
if (trackerEvent != null) {
|
||||
for (e in trackerEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Event.send(Leenkx.onTrackerEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onannounce = function() {
|
||||
if (announceEvent != null) {
|
||||
for (e in announceEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onAnnounceEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].onload = function() {
|
||||
if (loadEvent != null) {
|
||||
for (e in loadEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Event.send(Leenkx.onLoadEvent, uid);
|
||||
};
|
||||
Leenkx.connections[net_Url].ontorrentdone = function() {
|
||||
if (torrentDoneEvent != null) {
|
||||
for (e in torrentDoneEvent) {
|
||||
if (e.mask == object.uid) {
|
||||
e.onEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event.send(Leenkx.onTorrentDoneEvent, uid);
|
||||
};
|
||||
|
||||
#if js
|
||||
var lnxjs:Dynamic = js.Lib.global;
|
||||
var connectionUrl = net_Url;
|
||||
|
||||
if (lnxjs.Leenkx != null) {
|
||||
if (lnxjs.lnxNew == null) {
|
||||
js.Syntax.code('globalThis.lnxNew = function(url) { return new Leenkx(url); }');
|
||||
}
|
||||
Leenkx.connections[connectionUrl].onload();
|
||||
} else {
|
||||
kha.Assets.loadBlobFromPath("Leenkx.js", function(b: kha.Blob) {
|
||||
if (b != null) {
|
||||
js.Syntax.code("(1,eval)({0})", b.toString());
|
||||
if (lnxjs.Leenkx != null && lnxjs.lnxNew == null) {
|
||||
js.Syntax.code('globalThis.lnxNew = function(url) { return new Leenkx(url); }');
|
||||
}
|
||||
} else {
|
||||
trace("Warning: Leenkx.js blob is null - file may not be in assets");
|
||||
}
|
||||
Leenkx.connections[connectionUrl].onload();
|
||||
}, function(err: kha.AssetError) {
|
||||
trace("ERROR loading Leenkx.js: " + err.url + " - " + err.error);
|
||||
Leenkx.connections[connectionUrl].onload();
|
||||
});
|
||||
}
|
||||
#end
|
||||
Leenkx.connections[connectionUrl].onload();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -281,11 +120,11 @@ class Leenkx {
|
||||
|
||||
class LeenkxSocket {
|
||||
public var _url:String;
|
||||
public var client:haxe.DynamicAccess<Dynamic>;
|
||||
public var client:LeenkxClient;
|
||||
public var buffer:Dynamic;
|
||||
public var onopen:Void->Void;
|
||||
public var onclose:Void->Void;
|
||||
public var onmessage:Void->Void;
|
||||
public var onmessage:Void->Void;
|
||||
public var onerror:Void->Void;
|
||||
public var onseen:Void->Void;
|
||||
public var onserver:Void->Void;
|
||||
@ -293,11 +132,10 @@ class LeenkxSocket {
|
||||
public var onping:Void->Void;
|
||||
public var onleft:Void->Void;
|
||||
public var ontimeout:Void->Void;
|
||||
public var onrpc:Void->Void;
|
||||
public var onrpcresponse:Void->Void;
|
||||
public var onwireleft:Void->Void;
|
||||
public var onwireseen:Void->Void;
|
||||
public var ontorrent:Void->Void;
|
||||
public var ontorrentadded:Void->Void;
|
||||
public var ontracker:Void->Void;
|
||||
public var onannounce:Void->Void;
|
||||
public var onload:Void->Void;
|
||||
@ -308,3 +146,4 @@ class LeenkxSocket {
|
||||
}
|
||||
|
||||
}
|
||||
#end
|
||||
|
||||
@ -1,51 +0,0 @@
|
||||
package leenkx.network;
|
||||
|
||||
class Log {
|
||||
public static inline var INFO:Int = 0x000001;
|
||||
public static inline var DEBUG:Int = 0x000010;
|
||||
public static inline var DATA:Int = 0x000100;
|
||||
|
||||
public static var mask:Int = 0;
|
||||
|
||||
#if (sys || kha_krom)
|
||||
public static var logFn:Dynamic->Void = function(data:Dynamic) { trace(data); };
|
||||
#elseif js
|
||||
public static var logFn:Dynamic->Void = js.html.Console.log;
|
||||
#end
|
||||
|
||||
public static function info(data:String, id:String = null) {
|
||||
if (mask & INFO != INFO) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (id != null) {
|
||||
logFn('INFO :: ID-${id} :: ${data}');
|
||||
} else {
|
||||
logFn('INFO :: ${data}');
|
||||
}
|
||||
}
|
||||
|
||||
public static function debug(data:String, id:String = null) {
|
||||
if (mask & DEBUG != DEBUG) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (id != null) {
|
||||
logFn('DEBUG :: ID-${id} :: ${data}');
|
||||
} else {
|
||||
logFn('DEBUG :: ${data}');
|
||||
}
|
||||
}
|
||||
|
||||
public static function data(data:String, id:String = null) {
|
||||
if (mask & DATA != DATA) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (id != null) {
|
||||
logFn('DATA :: ID-${id}\n------------------------------\n${data}\n------------------------------');
|
||||
} else {
|
||||
logFn('${data}');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -136,18 +136,43 @@ import haxe.io.Bytes;
|
||||
|
||||
class WebSocket {
|
||||
private var _url:String;
|
||||
|
||||
private var _protocols:Array<String> = null;
|
||||
private var _ws:js.html.WebSocket = null;
|
||||
|
||||
public function new(url:String, immediateOpen=true) {
|
||||
public var state(get, null): State;
|
||||
function get_state() {
|
||||
if (_ws == null) return Closed;
|
||||
switch (_ws.readyState) {
|
||||
case js.html.WebSocket.CONNECTING:
|
||||
return Handshake;
|
||||
case js.html.WebSocket.OPEN:
|
||||
return Body;
|
||||
}
|
||||
|
||||
return Closed;
|
||||
}
|
||||
|
||||
public var protocol(get, null): String;
|
||||
function get_protocol() {
|
||||
if (_ws == null)
|
||||
return null;
|
||||
return _ws.protocol;
|
||||
}
|
||||
|
||||
public function new(url:String, immediateOpen=true, protocols:Array<String> = null) {
|
||||
_url = url;
|
||||
_protocols = protocols;
|
||||
if (immediateOpen) {
|
||||
open();
|
||||
}
|
||||
}
|
||||
|
||||
private function createSocket() {
|
||||
return new js.html.WebSocket(_url);
|
||||
if (_protocols == null) {
|
||||
return new js.html.WebSocket(_url);
|
||||
}
|
||||
|
||||
return new js.html.WebSocket(_url, _protocols);
|
||||
}
|
||||
|
||||
public function open() {
|
||||
@ -292,6 +317,10 @@ class WebSocket extends WebSocketCommon {
|
||||
public var _host:String;
|
||||
public var _port:Int = 0;
|
||||
public var _path:String;
|
||||
public var _search:String;
|
||||
|
||||
private var _protocols:Array<String>;
|
||||
public var protocol(default, null):String = null;
|
||||
|
||||
private var _processThread:Thread;
|
||||
private var _encodedKey:String = "wskey";
|
||||
@ -300,8 +329,9 @@ class WebSocket extends WebSocketCommon {
|
||||
|
||||
public var additionalHeaders(get, null):Map<String, String>;
|
||||
|
||||
public function new(url:String, immediateOpen=true) {
|
||||
public function new(url:String, immediateOpen=true, protocols:Array<String> = null) {
|
||||
parseUrl(url);
|
||||
_protocols = protocols;
|
||||
|
||||
super(createSocket());
|
||||
|
||||
@ -310,40 +340,42 @@ class WebSocket extends WebSocketCommon {
|
||||
}
|
||||
}
|
||||
|
||||
inline private function parseUrl(url)
|
||||
inline private function parseUrl(url:String)
|
||||
{
|
||||
/** TO DO - FIND OUT WHAT IS BREAKING REGEX IN THE NEW PCRE2 FOR HL C
|
||||
|
||||
var urlRegExp = ~/^(\w+?):\/\/([\w\.-]+)(:(\d+))?(\/.*)?$/;
|
||||
|
||||
if ( ! urlRegExp.match(url)) {
|
||||
var sep = url.indexOf("://");
|
||||
if (sep < 0) {
|
||||
throw 'Uri not matching websocket URL "${url}"';
|
||||
}
|
||||
_protocol = url.substr(0, sep);
|
||||
var rest = url.substr(sep + 3);
|
||||
|
||||
_protocol = urlRegExp.matched(1);
|
||||
var pathStart = rest.length;
|
||||
var slashIdx = rest.indexOf("/");
|
||||
var qIdx = rest.indexOf("?");
|
||||
if (slashIdx >= 0 && slashIdx < pathStart) pathStart = slashIdx;
|
||||
if (qIdx >= 0 && qIdx < pathStart) pathStart = qIdx;
|
||||
|
||||
_host = urlRegExp.matched(2);
|
||||
var hostPort = rest.substr(0, pathStart);
|
||||
var pathAndSearch = rest.substr(pathStart);
|
||||
|
||||
var parsedPort = Std.parseInt(urlRegExp.matched(4));
|
||||
if (parsedPort > 0 ) {
|
||||
_port = parsedPort;
|
||||
var colonIdx = hostPort.indexOf(":");
|
||||
if (colonIdx >= 0) {
|
||||
_host = hostPort.substr(0, colonIdx);
|
||||
var parsedPort = Std.parseInt(hostPort.substr(colonIdx + 1));
|
||||
if (parsedPort > 0) {
|
||||
_port = parsedPort;
|
||||
}
|
||||
} else {
|
||||
_host = hostPort;
|
||||
}
|
||||
_path = urlRegExp.matched(5);
|
||||
if (_path == null || _path.length == 0) {
|
||||
_path = "/";
|
||||
|
||||
_path = pathAndSearch;
|
||||
_search = null;
|
||||
var searchIdx = pathAndSearch.indexOf("?");
|
||||
if (searchIdx >= 0) {
|
||||
_path = pathAndSearch.substr(0, searchIdx);
|
||||
_search = pathAndSearch.substr(searchIdx);
|
||||
}
|
||||
**/
|
||||
var urlArr = url.split(":");
|
||||
if ( urlArr.length < 3) {
|
||||
throw 'Uri not matching websocket URL "${url}"';
|
||||
}
|
||||
_protocol = urlArr[0];
|
||||
_host = urlArr[1].substr(2, urlArr[1].length);
|
||||
var parsedPort = Std.parseInt(urlArr[2].split("/")[0]);
|
||||
if (parsedPort > 0 ) {
|
||||
_port = parsedPort;
|
||||
}
|
||||
_path = urlArr[2].substr(urlArr[2].split("/")[0].length, urlArr[2].length);
|
||||
if (_path == null || _path.length == 0) {
|
||||
_path = "/";
|
||||
}
|
||||
@ -388,9 +420,7 @@ class WebSocket extends WebSocketCommon {
|
||||
#else
|
||||
|
||||
haxe.MainLoop.addThread(function() {
|
||||
Log.debug("Thread started", this.id);
|
||||
processLoop(this);
|
||||
Log.debug("Thread ended", this.id);
|
||||
});
|
||||
|
||||
#end
|
||||
@ -399,16 +429,13 @@ class WebSocket extends WebSocketCommon {
|
||||
}
|
||||
|
||||
private function processThread() {
|
||||
Log.debug("Thread started", this.id);
|
||||
var ws:WebSocket = Thread.readMessage(true);
|
||||
processLoop(this);
|
||||
Log.debug("Thread ended", this.id);
|
||||
}
|
||||
|
||||
private function processLoop(ws:WebSocket) {
|
||||
while (ws.state != State.Closed) { // TODO: should think about mutex
|
||||
ws.process();
|
||||
Sys.sleep(.01);
|
||||
}
|
||||
}
|
||||
|
||||
@ -422,8 +449,7 @@ class WebSocket extends WebSocketCommon {
|
||||
public function sendHandshake() {
|
||||
var httpRequest = new HttpRequest();
|
||||
httpRequest.method = "GET";
|
||||
// TODO: should propably be hostname+port+path?
|
||||
httpRequest.uri = _path;
|
||||
httpRequest.uri = (_search != null) ? _path + _search : _path;
|
||||
httpRequest.httpVersion = "HTTP/1.1";
|
||||
|
||||
httpRequest.headers.set(HttpHeader.HOST, _host + ":" + _port);
|
||||
@ -435,6 +461,9 @@ class WebSocket extends WebSocketCommon {
|
||||
httpRequest.headers.set(HttpHeader.CACHE_CONTROL, "no-cache");
|
||||
httpRequest.headers.set(HttpHeader.ORIGIN, _socket.host().host.toString() + ":" + _socket.host().port);
|
||||
|
||||
if (_protocols != null) {
|
||||
httpRequest.headers.set(HttpHeader.SEC_WEBSOCKET_PROTOCOL, _protocols.join(', '));
|
||||
}
|
||||
|
||||
_encodedKey = generateWSKey();
|
||||
httpRequest.headers.set(HttpHeader.SEC_WEBSOCKET_KEY, _encodedKey);
|
||||
@ -487,16 +516,18 @@ class WebSocket extends WebSocketCommon {
|
||||
}
|
||||
}
|
||||
|
||||
var protocol = httpResponse.headers.get(HttpHeader.SEC_WEBSOCKET_PROTOCOL);
|
||||
if (protocol != null) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
_onopenCalled = false;
|
||||
state = State.Head;
|
||||
}
|
||||
|
||||
private function generateWSKey():String {
|
||||
var b = Bytes.alloc(16);
|
||||
for (i in 0...16) {
|
||||
b.set(i, Std.random(255));
|
||||
}
|
||||
return Base64.encode(b);
|
||||
return haxe.crypto.Base64.encode(
|
||||
leenkx.network.torrent.Crypto.randomBytes(16));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -38,7 +38,6 @@ class WebSocketCommon {
|
||||
|
||||
public function send(data:Any) {
|
||||
if (Std.isOfType(data, String)) {
|
||||
Log.data(data, id);
|
||||
sendFrame(Utf8Encoder.encode(data), OpCode.Text);
|
||||
} else if (Std.isOfType(data, Bytes)) {
|
||||
sendFrame(data, OpCode.Binary);
|
||||
@ -118,7 +117,6 @@ class WebSocketCommon {
|
||||
}
|
||||
} else {
|
||||
var stringPayload = Utf8Encoder.decode(unmaskedMessageData);
|
||||
Log.data(stringPayload, id);
|
||||
if (this.onmessage != null) {
|
||||
this.onmessage(StrMessage(stringPayload));
|
||||
}
|
||||
@ -138,14 +136,12 @@ class WebSocketCommon {
|
||||
case State.Closed:
|
||||
close();
|
||||
case _:
|
||||
trace('State not impl: ${state}');
|
||||
}
|
||||
}
|
||||
|
||||
public function close() {
|
||||
if (state != State.Closed) {
|
||||
try {
|
||||
Log.debug("Closed", id);
|
||||
sendFrame(Bytes.alloc(0), OpCode.Close);
|
||||
state = State.Closed;
|
||||
_socket.close();
|
||||
@ -162,7 +158,6 @@ class WebSocketCommon {
|
||||
_socket.output.write(data);
|
||||
_socket.output.flush();
|
||||
} catch (e:Dynamic) {
|
||||
Log.debug(Std.string(e), id);
|
||||
if (onerror != null) {
|
||||
onerror(Std.string(e));
|
||||
}
|
||||
@ -195,17 +190,24 @@ class WebSocketCommon {
|
||||
}
|
||||
|
||||
private static function generateMask() {
|
||||
var maskData = Bytes.alloc(4);
|
||||
maskData.set(0, Std.random(256));
|
||||
maskData.set(1, Std.random(256));
|
||||
maskData.set(2, Std.random(256));
|
||||
maskData.set(3, Std.random(256));
|
||||
return maskData;
|
||||
return leenkx.network.torrent.Crypto.randomBytes(4);
|
||||
}
|
||||
|
||||
private static function applyMask(payload:Bytes, mask:Bytes) {
|
||||
var maskedPayload = Bytes.alloc(payload.length);
|
||||
for (n in 0 ... payload.length) maskedPayload.set(n, payload.get(n) ^ mask.get(n % mask.length));
|
||||
var m0 = mask.get(0), m1 = mask.get(1), m2 = mask.get(2), m3 = mask.get(3);
|
||||
var i = 0;
|
||||
while (i + 3 < payload.length) {
|
||||
maskedPayload.set(i, payload.get(i) ^ m0);
|
||||
maskedPayload.set(i + 1, payload.get(i + 1) ^ m1);
|
||||
maskedPayload.set(i + 2, payload.get(i + 2) ^ m2);
|
||||
maskedPayload.set(i + 3, payload.get(i + 3) ^ m3);
|
||||
i += 4;
|
||||
}
|
||||
while (i < payload.length) {
|
||||
maskedPayload.set(i, payload.get(i) ^ mask.get(i & 3));
|
||||
i++;
|
||||
}
|
||||
return maskedPayload;
|
||||
}
|
||||
|
||||
@ -230,7 +232,6 @@ class WebSocketCommon {
|
||||
try {
|
||||
result = SocketImpl.select([_socket], null, null, 0.01);
|
||||
} catch (e:Dynamic) {
|
||||
Log.debug("Error selecting socket: " + e);
|
||||
needClose = true;
|
||||
}
|
||||
|
||||
@ -238,12 +239,11 @@ class WebSocketCommon {
|
||||
if (result.read.length > 0) {
|
||||
try {
|
||||
while (true) {
|
||||
var data = Bytes.alloc(1024);
|
||||
var data = Bytes.alloc(65536);
|
||||
var read = _socket.input.readBytes(data, 0, data.length);
|
||||
if (read <= 0){
|
||||
break;
|
||||
}
|
||||
Log.debug("Bytes read: " + read, id);
|
||||
_buffer.writeBytes(data.sub(0, read));
|
||||
}
|
||||
} catch (e:Dynamic) {
|
||||
@ -259,7 +259,7 @@ class WebSocketCommon {
|
||||
}
|
||||
#else
|
||||
|
||||
needClose = !(e == 'Blocking' || (Std.isOfType(e, Error) && (e:Error).match(Error.Blocked)));
|
||||
needClose = !(e == 'Blocking' || (Std.isOfType(e, Error) && (e:Error).match(Error.Blocked)) || (state == Handshake && e == haxe.io.Eof));
|
||||
|
||||
#end
|
||||
}
|
||||
@ -273,7 +273,6 @@ class WebSocketCommon {
|
||||
if (needClose == true) { // dont want to send the Close frame here
|
||||
if (state != State.Closed) {
|
||||
try {
|
||||
Log.debug("Closed", id);
|
||||
state = State.Closed;
|
||||
_socket.close();
|
||||
} catch (e:Dynamic) { }
|
||||
@ -288,8 +287,6 @@ class WebSocketCommon {
|
||||
public function sendHttpRequest(httpRequest:HttpRequest) {
|
||||
var data = httpRequest.build();
|
||||
|
||||
Log.data(data, id);
|
||||
|
||||
try {
|
||||
_socket.output.write(Bytes.ofString(data));
|
||||
_socket.output.flush();
|
||||
@ -304,8 +301,6 @@ class WebSocketCommon {
|
||||
public function sendHttpResponse(httpResponse:HttpResponse) {
|
||||
var data = httpResponse.build();
|
||||
|
||||
Log.data(data, id);
|
||||
|
||||
_socket.output.write(Bytes.ofString(data));
|
||||
_socket.output.flush();
|
||||
}
|
||||
@ -325,8 +320,6 @@ class WebSocketCommon {
|
||||
httpRequest.addLine(line);
|
||||
}
|
||||
|
||||
Log.data(httpRequest.toString(), id);
|
||||
|
||||
return httpRequest;
|
||||
}
|
||||
|
||||
@ -346,8 +339,6 @@ class WebSocketCommon {
|
||||
|
||||
}
|
||||
|
||||
Log.data(httpResponse.toString(), id);
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
|
||||
@ -13,7 +13,6 @@ class WebSocketHandler extends Handler {
|
||||
_creationTime = Sys.time();
|
||||
#end
|
||||
_socket.setBlocking(false);
|
||||
Log.debug('New socket handler', id);
|
||||
}
|
||||
|
||||
public override function handle() {
|
||||
@ -23,7 +22,6 @@ class WebSocketHandler extends Handler {
|
||||
var currentTime = Sys.time();
|
||||
#end
|
||||
if (this.state == State.Handshake && currentTime - _creationTime > (MAX_WAIT_TIME / 1000)) {
|
||||
Log.info('No handshake detected in ${MAX_WAIT_TIME}ms, closing connection', id);
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
@ -70,10 +68,8 @@ class WebSocketHandler extends Handler {
|
||||
httpResponse.headers.set(HttpHeader.CONNECTION, "close");
|
||||
httpResponse.headers.set(HttpHeader.X_WEBSOCKET_REJECT_REASON, 'Unsupported connection header: ${httpRequest.headers.get(HttpHeader.CONNECTION)}.');
|
||||
} else {
|
||||
Log.debug('Handshaking', id);
|
||||
var key = httpRequest.headers.get(HttpHeader.SEC_WEBSOCKET_KEY);
|
||||
var result = makeWSKeyResponse(key);
|
||||
Log.debug('Handshaking key - ${result}', id);
|
||||
|
||||
httpResponse.code = 101;
|
||||
httpResponse.text = "Switching Protocols";
|
||||
@ -82,14 +78,21 @@ class WebSocketHandler extends Handler {
|
||||
httpResponse.headers.set(HttpHeader.SEC_WEBSOSCKET_ACCEPT, result);
|
||||
}
|
||||
|
||||
sendHttpResponse(httpResponse);
|
||||
function callback(httpResponse: HttpResponse) {
|
||||
sendHttpResponse(httpResponse);
|
||||
|
||||
if (httpResponse.code == 101) {
|
||||
_onopenCalled = false;
|
||||
state = State.Head;
|
||||
Log.debug('Connected', id);
|
||||
if (httpResponse.code == 101) {
|
||||
_onopenCalled = false;
|
||||
state = State.Head;
|
||||
} else {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
if (validateHandshake != null) {
|
||||
validateHandshake(httpRequest, httpResponse, callback);
|
||||
} else {
|
||||
close();
|
||||
callback(httpResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +60,6 @@ class WebSocketServer
|
||||
_serverSocket.bind(new sys.net.Host(_host), _port);
|
||||
#end
|
||||
_serverSocket.listen(_maxConnections);
|
||||
Log.info('Starting server - ${_host}:${_port} (maxConnections: ${_maxConnections})');
|
||||
|
||||
#if kha_krom
|
||||
kha.Scheduler.addTimeTask(function() {
|
||||
@ -114,7 +113,6 @@ class WebSocketServer
|
||||
var handler = new T(socket);
|
||||
handlers.push(handler);
|
||||
|
||||
Log.debug("Adding to web server handler to list - total: " + handlers.length, handler.id);
|
||||
if (onClientAdded != null) {
|
||||
onClientAdded(handler);
|
||||
}
|
||||
@ -174,7 +172,6 @@ class WebSocketServer
|
||||
|
||||
for (h in toRemove) {
|
||||
handlers.remove(h);
|
||||
Log.debug("Removing web server handler from list - total: " + handlers.length, h.id);
|
||||
if (onClientRemoved != null) {
|
||||
onClientRemoved(h);
|
||||
}
|
||||
@ -186,7 +183,6 @@ class WebSocketServer
|
||||
while (_handlersClosed.length > 0) {
|
||||
var h = _handlersClosed.shift();
|
||||
handlers.remove(h);
|
||||
Log.debug("Removing web server handler from list - total: " + handlers.length, h.id);
|
||||
if (onClientRemoved != null) {
|
||||
onClientRemoved(h);
|
||||
}
|
||||
|
||||
@ -52,8 +52,15 @@ class KromSecureSocket extends KromSocket {
|
||||
}
|
||||
|
||||
override public function connect(host:KromHost, port:Int):Void {
|
||||
super.connect(host, port);
|
||||
enableSSL();
|
||||
setBlocking(true);
|
||||
try {
|
||||
super.connect(host, port);
|
||||
enableSSL();
|
||||
setBlocking(false);
|
||||
} catch (e:Dynamic) {
|
||||
setBlocking(false);
|
||||
trace('Failed Krom Host');
|
||||
}
|
||||
}
|
||||
|
||||
public function isSSLEnabled():Bool {
|
||||
|
||||
@ -43,6 +43,9 @@ class KromSocket {
|
||||
}
|
||||
|
||||
private function setSocketId(id:Int):Void {
|
||||
if (_socketId >= 0) {
|
||||
krom_socket_close(_socketId);
|
||||
}
|
||||
_socketId = id;
|
||||
if (_socketId >= 0) {
|
||||
input = new KromSocketInput(this);
|
||||
@ -247,3 +250,29 @@ class KromHost {
|
||||
return host;
|
||||
}
|
||||
}
|
||||
// TODO: Replace all ticks timers and intervals for events
|
||||
class KromPump {
|
||||
static var ticks:Array<Void -> Void> = [];
|
||||
static var timer:haxe.Timer = null;
|
||||
|
||||
public static function add(tick:Void -> Void):Void {
|
||||
if (!ticks.contains(tick)) ticks.push(tick);
|
||||
if (timer == null) {
|
||||
timer = new haxe.Timer(10);
|
||||
timer.run = runAll;
|
||||
}
|
||||
}
|
||||
public static function remove(tick:Void -> Void):Void {
|
||||
ticks.remove(tick);
|
||||
if (ticks.length == 0 && timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
}
|
||||
static function runAll():Void {
|
||||
var snapshot = ticks.copy();
|
||||
for (tick in snapshot) {
|
||||
if (ticks.contains(tick)) tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
70
leenkx/Sources/leenkx/network/krom/KromUdpSocket.hx
Normal file
70
leenkx/Sources/leenkx/network/krom/KromUdpSocket.hx
Normal file
@ -0,0 +1,70 @@
|
||||
package leenkx.network.krom;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
@:native("krom_udp_create") extern function krom_udp_create():Int;
|
||||
@:native("krom_udp_bind") extern function krom_udp_bind(id:Int,
|
||||
addr:String, port:Int):Bool;
|
||||
@:native("krom_udp_sendto") extern function krom_udp_sendto(id:Int,
|
||||
host:String, port:Int, data:js.lib.ArrayBuffer):Int;
|
||||
@:native("krom_udp_recvfrom") extern function krom_udp_recvfrom(id:Int,
|
||||
maxLen:Int):Dynamic;
|
||||
@:native("krom_socket_close") extern function krom_udp_close(id:Int):Void;
|
||||
@:native("krom_socket_set_blocking")
|
||||
extern function krom_udp_set_blocking(id:Int, blocking:Bool):Void;
|
||||
|
||||
class KromUdpSocket {
|
||||
private var _socketId:Int = -1;
|
||||
|
||||
public function new() {
|
||||
_socketId = krom_udp_create();
|
||||
setBlocking(false);
|
||||
}
|
||||
|
||||
public function bind(host:KromSocket.KromHost, port:Int):Void {
|
||||
if (_socketId >= 0) {
|
||||
if (!krom_udp_bind(_socketId, host.host, port)) {
|
||||
trace("UDP bind failed to " + host.host + ":" + port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function sendTo(data:Bytes, pos:Int, len:Int,
|
||||
host:KromSocket.KromHost, port:Int):Int {
|
||||
if (_socketId < 0) return 0;
|
||||
var arrayBuffer:js.lib.ArrayBuffer = data.getData();
|
||||
var exact:js.lib.ArrayBuffer =
|
||||
untyped arrayBuffer.slice(pos, pos + len);
|
||||
return krom_udp_sendto(_socketId, host.host, port, exact);
|
||||
}
|
||||
|
||||
public function recvFrom(maxLength:Int):{data:Bytes, host:String,
|
||||
port:Int} {
|
||||
if (_socketId < 0) return null;
|
||||
var result:Dynamic = krom_udp_recvfrom(_socketId, maxLength);
|
||||
if (result == -1) {
|
||||
_socketId = -1;
|
||||
trace("Closed");
|
||||
return null;
|
||||
}
|
||||
if (result == null || Std.isOfType(result, Int)) return null;
|
||||
return {
|
||||
data: Bytes.ofData(result.data),
|
||||
host: result.host,
|
||||
port: result.port
|
||||
};
|
||||
}
|
||||
|
||||
public function setBlocking(blocking:Bool):Void {
|
||||
if (_socketId >= 0) {
|
||||
krom_udp_set_blocking(_socketId, blocking);
|
||||
}
|
||||
}
|
||||
|
||||
public function close():Void {
|
||||
if (_socketId >= 0) {
|
||||
krom_udp_close(_socketId);
|
||||
_socketId = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
209
leenkx/Sources/leenkx/network/torrent/Aegis128x4.hx
Normal file
209
leenkx/Sources/leenkx/network/torrent/Aegis128x4.hx
Normal file
@ -0,0 +1,209 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class Aegis128x4 {
|
||||
static inline var D = 4;
|
||||
static inline var RATE = 128;
|
||||
|
||||
static var C0 = Bytes.ofHex("000101020305080d1522375990e97962");
|
||||
static var C1 = Bytes.ofHex("db3d18556dc22ff12011314273b528dd");
|
||||
|
||||
var v:Bytes;
|
||||
var nv:Bytes;
|
||||
var t:Bytes;
|
||||
var t2:Bytes;
|
||||
|
||||
public function new(key:Bytes, nonce:Bytes) {
|
||||
v = Bytes.alloc(512);
|
||||
nv = Bytes.alloc(512);
|
||||
t = Bytes.alloc(16);
|
||||
t2 = Bytes.alloc(16);
|
||||
init(key, nonce);
|
||||
}
|
||||
|
||||
static inline function off(j:Int, i:Int):Int {
|
||||
return (j * D + i) * 16;
|
||||
}
|
||||
|
||||
static inline function xtime(x:Int):Int {
|
||||
return ((x << 1) ^ ((x >> 7) * 0x1b)) & 0xff;
|
||||
}
|
||||
|
||||
static function xorInto(a:Bytes, ao:Int, b:Bytes, bo:Int, out:Bytes, oo:Int):Void {
|
||||
for (k in 0...16) out.set(oo + k, a.get(ao + k) ^ b.get(bo + k));
|
||||
}
|
||||
|
||||
function aesRound(inOff:Int, rk:Bytes, rkOff:Int, out:Bytes, outOff:Int):Void {
|
||||
for (c in 0...4) for (r in 0...4)
|
||||
t2.set(4 * c + r, SBOX[v.get(inOff + 4 * ((c + r) & 3) + r)]);
|
||||
for (c in 0...4) {
|
||||
var b0 = t2.get(4 * c);
|
||||
var b1 = t2.get(4 * c + 1);
|
||||
var b2 = t2.get(4 * c + 2);
|
||||
var b3 = t2.get(4 * c + 3);
|
||||
out.set(outOff + 4 * c, (xtime(b0) ^ xtime(b1) ^ b1 ^ b2 ^ b3) ^ rk.get(rkOff + 4 * c));
|
||||
out.set(outOff + 4 * c + 1, (b0 ^ xtime(b1) ^ xtime(b2) ^ b2 ^ b3) ^ rk.get(rkOff + 4 * c + 1));
|
||||
out.set(outOff + 4 * c + 2, (b0 ^ b1 ^ xtime(b2) ^ xtime(b3) ^ b3) ^ rk.get(rkOff + 4 * c + 2));
|
||||
out.set(outOff + 4 * c + 3, (xtime(b0) ^ b0 ^ b1 ^ b2 ^ xtime(b3)) ^ rk.get(rkOff + 4 * c + 3));
|
||||
}
|
||||
}
|
||||
|
||||
function update(m0:Bytes, m0off:Int, m1:Bytes, m1off:Int):Void {
|
||||
for (i in 0...D) {
|
||||
xorInto(v, off(0, i), m0, m0off + i * 16, t, 0);
|
||||
aesRound(off(7, i), t, 0, nv, off(0, i));
|
||||
aesRound(off(0, i), v, off(1, i), nv, off(1, i));
|
||||
aesRound(off(1, i), v, off(2, i), nv, off(2, i));
|
||||
aesRound(off(2, i), v, off(3, i), nv, off(3, i));
|
||||
xorInto(v, off(4, i), m1, m1off + i * 16, t, 0);
|
||||
aesRound(off(3, i), t, 0, nv, off(4, i));
|
||||
aesRound(off(4, i), v, off(5, i), nv, off(5, i));
|
||||
aesRound(off(5, i), v, off(6, i), nv, off(6, i));
|
||||
aesRound(off(6, i), v, off(7, i), nv, off(7, i));
|
||||
}
|
||||
v.blit(0, nv, 0, 512);
|
||||
}
|
||||
|
||||
function init(key:Bytes, nonce:Bytes):Void {
|
||||
var kn = Bytes.alloc(16);
|
||||
var kc0 = Bytes.alloc(16);
|
||||
var kc1 = Bytes.alloc(16);
|
||||
for (k in 0...16) {
|
||||
kn.set(k, key.get(k) ^ nonce.get(k));
|
||||
kc0.set(k, key.get(k) ^ C0.get(k));
|
||||
kc1.set(k, key.get(k) ^ C1.get(k));
|
||||
}
|
||||
for (i in 0...D) {
|
||||
v.blit(off(0, i), kn, 0, 16);
|
||||
v.blit(off(1, i), C1, 0, 16);
|
||||
v.blit(off(2, i), C0, 0, 16);
|
||||
v.blit(off(3, i), C1, 0, 16);
|
||||
v.blit(off(4, i), kn, 0, 16);
|
||||
v.blit(off(5, i), kc0, 0, 16);
|
||||
v.blit(off(6, i), kc1, 0, 16);
|
||||
v.blit(off(7, i), kc0, 0, 16);
|
||||
}
|
||||
var m0 = Bytes.alloc(64);
|
||||
var m1 = Bytes.alloc(64);
|
||||
for (i in 0...D) {
|
||||
m0.blit(i * 16, nonce, 0, 16);
|
||||
m1.blit(i * 16, key, 0, 16);
|
||||
}
|
||||
for (r in 0...10) {
|
||||
for (i in 0...D) {
|
||||
v.set(off(3, i), v.get(off(3, i)) ^ i);
|
||||
v.set(off(3, i) + 1, v.get(off(3, i) + 1) ^ (D - 1));
|
||||
v.set(off(7, i), v.get(off(7, i)) ^ i);
|
||||
v.set(off(7, i) + 1, v.get(off(7, i) + 1) ^ (D - 1));
|
||||
}
|
||||
update(m0, 0, m1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public function absorb(ai:Bytes, aiOff:Int):Void {
|
||||
update(ai, aiOff, ai, aiOff + 64);
|
||||
}
|
||||
|
||||
public function enc(xi:Bytes, xiOff:Int):Bytes {
|
||||
var out = Bytes.alloc(RATE);
|
||||
for (i in 0...D) {
|
||||
for (k in 0...16) {
|
||||
var z0 = v.get(off(1, i) + k) ^ v.get(off(6, i) + k)
|
||||
^ (v.get(off(2, i) + k) & v.get(off(3, i) + k));
|
||||
var z1 = v.get(off(2, i) + k) ^ v.get(off(5, i) + k)
|
||||
^ (v.get(off(6, i) + k) & v.get(off(7, i) + k));
|
||||
out.set(i * 16 + k, xi.get(xiOff + i * 16 + k) ^ z0);
|
||||
out.set(64 + i * 16 + k, xi.get(xiOff + 64 + i * 16 + k) ^ z1);
|
||||
}
|
||||
}
|
||||
update(xi, xiOff, xi, xiOff + 64);
|
||||
return out;
|
||||
}
|
||||
|
||||
public function nextBlock():Bytes {
|
||||
var out = Bytes.alloc(RATE);
|
||||
for (i in 0...D) {
|
||||
for (k in 0...16) {
|
||||
out.set(i * 16 + k, v.get(off(1, i) + k) ^ v.get(off(6, i) + k)
|
||||
^ (v.get(off(2, i) + k) & v.get(off(3, i) + k)));
|
||||
out.set(64 + i * 16 + k, v.get(off(2, i) + k) ^ v.get(off(5, i) + k)
|
||||
^ (v.get(off(6, i) + k) & v.get(off(7, i) + k)));
|
||||
}
|
||||
}
|
||||
for (i in 0...D) {
|
||||
aesRound(off(7, i), v, off(0, i), nv, off(0, i));
|
||||
aesRound(off(0, i), v, off(1, i), nv, off(1, i));
|
||||
aesRound(off(1, i), v, off(2, i), nv, off(2, i));
|
||||
aesRound(off(2, i), v, off(3, i), nv, off(3, i));
|
||||
aesRound(off(3, i), v, off(4, i), nv, off(4, i));
|
||||
aesRound(off(4, i), v, off(5, i), nv, off(5, i));
|
||||
aesRound(off(5, i), v, off(6, i), nv, off(6, i));
|
||||
aesRound(off(6, i), v, off(7, i), nv, off(7, i));
|
||||
}
|
||||
v.blit(0, nv, 0, 512);
|
||||
return out;
|
||||
}
|
||||
|
||||
public static function selfTest():Bool {
|
||||
var key = Bytes.ofHex("000102030405060708090a0b0c0d0e0f");
|
||||
var nonce = Bytes.ofHex("101112131415161718191a1b1c1d1e1f");
|
||||
var a = new Aegis128x4(key, nonce);
|
||||
var expected = Bytes.ofHex(
|
||||
"924eb07635003a37e6c6575ba8ce1929c8b6a5d91475445e936d48e794be0ce2" +
|
||||
"fcd37d050e24084befe3bbb219d647602e9f58cfb893a8800220242c373a8b18" +
|
||||
"1a1f60c4fab64e5471dc72edfcf6fe6bc1e525ebea2d6375a9edd045dce96381" +
|
||||
"97a3e25abd228a44d4a14a6d3fe9185cc2d4cf7f4287a98744645674265d4ca8" +
|
||||
"7bb50c534f6ec4780530ff1cce8a16e87b08d57557da0b5ef7b5f7d98b0ba189" +
|
||||
"6bfcac34ddb68404821a4d665303cb0fd95626f6dfad1aed7467622c38529932" +
|
||||
"af339fd2d50ee45fc47665c647cf6586d0669b39d140f0e118a4a511efe2f95a" +
|
||||
"7a94330f35c194fadda2a87e42cdeccc233b640d1f4d56e2757e72c1a9d8ecb1" +
|
||||
"9f93737d699ba05c11e94f2b201bef5e61caf387cf7cfd3f8300ac7680ccfd76" +
|
||||
"5825a671ecef03b7a9c98a601ae3211587a1fe4d558161a8f4c38731f3223032" +
|
||||
"7a5aca78d636c05bbc702b2980196ab6915d868408495d07eb527789f282c575" +
|
||||
"d0947bfbc1d3309cdffc9be1503aea628834ea57a15b9fbdc0245464a4b8cbef" +
|
||||
"e46f4cf71a95ac45b6f0823e3aba1a868c4ecef682fc44a8eba911b3fc7d99f9" +
|
||||
"a4fb61e2c928a2ca760b8772f2ea5f2e3d34ea89da73caa3016c280500a155a3" +
|
||||
"85075f0080e9d618e7eb40f57c32d9f7d2ab2b320c6e93b155a3787cb83e5281" +
|
||||
"0b3af0250ae36831a1b072e499929bcb5cce4d00329d69f1aae36aa541347512");
|
||||
if (expected.compare(a.v) != 0) {
|
||||
return false;
|
||||
}
|
||||
var ad = Bytes.alloc(128);
|
||||
for (k in 0...8) ad.set(k, [1, 2, 3, 4][k & 3]);
|
||||
a.absorb(ad, 0);
|
||||
var msg = Bytes.alloc(128);
|
||||
for (k in 0...120) msg.set(k, [4, 5, 6, 7][k & 3]);
|
||||
var ct = a.enc(msg, 0);
|
||||
var expectedCt = Bytes.ofHex(
|
||||
"e836118562f4479c9d35c17356a833114c21f9aa39e4dda5e5c87f4152a00fce" +
|
||||
"9a7c38f832eafe8b1c12f8a7cf12a81a1ad8a9c24ba9dedfbdaa586ffea67ddc" +
|
||||
"801ea97d9ab4a872f42d0e352e2713dacd609f9442c17517c5a29daf3e2a3fac" +
|
||||
"4ff6b1380c4e46df7b086af6ce6bc1ed594b8dd64aed2a7e");
|
||||
for (k in 0...120) {
|
||||
if (ct.get(k) != expectedCt.get(k)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static var SBOX = [
|
||||
0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
|
||||
0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
|
||||
0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
|
||||
0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
|
||||
0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
|
||||
0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
|
||||
0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
|
||||
0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
|
||||
0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
|
||||
0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
|
||||
0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
|
||||
0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
|
||||
0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
|
||||
0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
|
||||
0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
|
||||
0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16
|
||||
];
|
||||
}
|
||||
119
leenkx/Sources/leenkx/network/torrent/Base58.hx
Normal file
119
leenkx/Sources/leenkx/network/torrent/Base58.hx
Normal file
@ -0,0 +1,119 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class Base58 {
|
||||
|
||||
static var ALPHABET:String = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
static var ALPHABET_MAP:Map<Int, Int> = {
|
||||
var m = new Map<Int, Int>();
|
||||
for (i in 0...ALPHABET.length) {
|
||||
m.set(ALPHABET.charCodeAt(i), i);
|
||||
}
|
||||
m;
|
||||
};
|
||||
|
||||
public static function encode(bytes:Bytes):String {
|
||||
if (bytes.length == 0) return "";
|
||||
|
||||
var zeros = 0;
|
||||
while (zeros < bytes.length && bytes.get(zeros) == 0) zeros++;
|
||||
|
||||
var size = Math.ceil(bytes.length * 138 / 100 + 1);
|
||||
var b58 = [for (i in 0...size) 0];
|
||||
|
||||
for (i in zeros...bytes.length) {
|
||||
var carry = bytes.get(i);
|
||||
for (j in 0...size) {
|
||||
var idx = size - 1 - j;
|
||||
carry += b58[idx] << 8;
|
||||
b58[idx] = carry % 58;
|
||||
carry = Std.int(carry / 58);
|
||||
}
|
||||
}
|
||||
|
||||
var leadingZeros = 0;
|
||||
for (i in 0...size) {
|
||||
if (b58[i] != 0) break;
|
||||
leadingZeros++;
|
||||
}
|
||||
|
||||
var sb = new StringBuf();
|
||||
for (i in 0...zeros) sb.addChar(ALPHABET.charCodeAt(0));
|
||||
for (i in leadingZeros...size) {
|
||||
sb.addChar(ALPHABET.charCodeAt(b58[i]));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static function decode(str:String):Bytes {
|
||||
if (str.length == 0) return Bytes.alloc(0);
|
||||
|
||||
var zeros = 0;
|
||||
while (zeros < str.length && str.charCodeAt(zeros) == ALPHABET.charCodeAt(0)) zeros++;
|
||||
|
||||
var size = Math.ceil(str.length * 733 / 1000 + 1);
|
||||
var bytes = [for (i in 0...size) 0];
|
||||
|
||||
for (i in zeros...str.length) {
|
||||
var ch = str.charCodeAt(i);
|
||||
if (!ALPHABET_MAP.exists(ch)) {
|
||||
throw "Base58: invalid character " + str.charAt(i);
|
||||
}
|
||||
var carry = ALPHABET_MAP.get(ch);
|
||||
for (j in 0...size) {
|
||||
var idx = size - 1 - j;
|
||||
carry += bytes[idx] * 58;
|
||||
bytes[idx] = carry & 0xff;
|
||||
carry = carry >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
var leadingZeros = 0;
|
||||
for (i in 0...size) {
|
||||
if (bytes[i] != 0) break;
|
||||
leadingZeros++;
|
||||
}
|
||||
|
||||
var result = Bytes.alloc(zeros + size - leadingZeros);
|
||||
for (i in 0...zeros) result.set(i, 0);
|
||||
for (i in 0...(size - leadingZeros)) {
|
||||
result.set(zeros + i, bytes[leadingZeros + i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
class Base58Check {
|
||||
|
||||
static var CHECKSUM_LEN = 4;
|
||||
|
||||
public static function encode(bytes:Bytes):String {
|
||||
var hash = sha256sha256(bytes);
|
||||
var payload = Bytes.alloc(bytes.length + CHECKSUM_LEN);
|
||||
payload.blit(0, bytes, 0, bytes.length);
|
||||
payload.blit(bytes.length, hash, 0, CHECKSUM_LEN);
|
||||
return Base58.encode(payload);
|
||||
}
|
||||
|
||||
public static function decode(str:String):Bytes {
|
||||
var payload = Base58.decode(str);
|
||||
if (payload.length < CHECKSUM_LEN) {
|
||||
throw "Base58Check: too short";
|
||||
}
|
||||
var data = payload.sub(0, payload.length - CHECKSUM_LEN);
|
||||
var checksum = payload.sub(payload.length - CHECKSUM_LEN, CHECKSUM_LEN);
|
||||
var hash = sha256sha256(data);
|
||||
for (i in 0...CHECKSUM_LEN) {
|
||||
if (hash.get(i) != checksum.get(i)) {
|
||||
throw "Base58Check: checksum mismatch";
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
static function sha256sha256(bytes:Bytes):Bytes {
|
||||
var h1 = haxe.crypto.Sha256.make(bytes);
|
||||
return haxe.crypto.Sha256.make(h1);
|
||||
}
|
||||
}
|
||||
182
leenkx/Sources/leenkx/network/torrent/Bencode.hx
Normal file
182
leenkx/Sources/leenkx/network/torrent/Bencode.hx
Normal file
@ -0,0 +1,182 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class RawDict {
|
||||
public var pairs:Array<{key:Bytes, val:Dynamic}> = [];
|
||||
public function new() {}
|
||||
}
|
||||
|
||||
class Bencode {
|
||||
|
||||
public static function encode(value:Dynamic):Bytes {
|
||||
var out = new haxe.io.BytesOutput();
|
||||
encodeValue(out, value);
|
||||
return out.getBytes();
|
||||
}
|
||||
|
||||
public static function encodeToString(value:Dynamic):String {
|
||||
return encode(value).toString();
|
||||
}
|
||||
|
||||
static function encodeValue(out:haxe.io.BytesOutput, value:Dynamic):Void {
|
||||
if (Std.isOfType(value, Bool)) {
|
||||
encodeInt(out, value ? 1 : 0);
|
||||
} else if (Std.isOfType(value, Int) || Std.isOfType(value, Float)) {
|
||||
encodeInt(out, value);
|
||||
} else if (Std.isOfType(value, String)) {
|
||||
encodeString(out, value);
|
||||
} else if (Std.isOfType(value, Bytes)) {
|
||||
encodeBytes(out, value);
|
||||
} else if (Std.isOfType(value, RawDict)) {
|
||||
encodeRawDict(out, value);
|
||||
} else if (Std.isOfType(value, Array)) {
|
||||
encodeList(out, value);
|
||||
} else if (value != null && Reflect.isObject(value)) {
|
||||
encodeDict(out, value);
|
||||
} else {
|
||||
throw "Bencode: cannot encode " + Type.typeof(value);
|
||||
}
|
||||
}
|
||||
|
||||
static function encodeInt(out:haxe.io.BytesOutput, value:Float):Void {
|
||||
out.writeString("i");
|
||||
var s = Std.string(value);
|
||||
var dot = s.indexOf(".");
|
||||
if (dot != -1) s = s.substring(0, dot);
|
||||
if (s.indexOf("e") != -1 || s.indexOf("E") != -1) {
|
||||
s = Std.string(Math.floor(value));
|
||||
dot = s.indexOf(".");
|
||||
if (dot != -1) s = s.substring(0, dot);
|
||||
}
|
||||
out.writeString(s);
|
||||
out.writeString("e");
|
||||
}
|
||||
|
||||
static function encodeString(out:haxe.io.BytesOutput, value:String):Void {
|
||||
var bytes = Bytes.ofString(value);
|
||||
encodeBytes(out, bytes);
|
||||
}
|
||||
|
||||
static function encodeBytes(out:haxe.io.BytesOutput, bytes:Bytes):Void {
|
||||
out.writeString(Std.string(bytes.length));
|
||||
out.writeString(":");
|
||||
out.writeBytes(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
static function encodeRawDict(out:haxe.io.BytesOutput, dict:RawDict):Void {
|
||||
out.writeString("d");
|
||||
for (p in dict.pairs) {
|
||||
encodeBytes(out, p.key);
|
||||
encodeValue(out, p.val);
|
||||
}
|
||||
out.writeString("e");
|
||||
}
|
||||
|
||||
static function encodeList(out:haxe.io.BytesOutput, arr:Array<Dynamic>):Void {
|
||||
out.writeString("l");
|
||||
for (item in arr) {
|
||||
encodeValue(out, item);
|
||||
}
|
||||
out.writeString("e");
|
||||
}
|
||||
|
||||
static function encodeDict(out:haxe.io.BytesOutput, obj:Dynamic):Void {
|
||||
out.writeString("d");
|
||||
var keys = Reflect.fields(obj);
|
||||
keys.sort(Reflect.compare);
|
||||
for (key in keys) {
|
||||
if (Reflect.field(obj, key) == null) continue;
|
||||
encodeString(out, key);
|
||||
encodeValue(out, Reflect.field(obj, key));
|
||||
}
|
||||
out.writeString("e");
|
||||
}
|
||||
|
||||
public static function decode(bytes:Bytes):Dynamic {
|
||||
return new Decoder(bytes).value();
|
||||
}
|
||||
|
||||
public static function decodeString(str:String):Dynamic {
|
||||
return decode(Bytes.ofString(str));
|
||||
}
|
||||
}
|
||||
|
||||
private class Decoder {
|
||||
static inline var MAX_DECODE_LEN = 10 * 1024 * 1024; // 10 MB
|
||||
|
||||
var data:Bytes;
|
||||
var pos:Int = 0;
|
||||
|
||||
public function new(data:Bytes) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public function value():Dynamic {
|
||||
if (pos >= data.length) throw "Bencode: truncated input";
|
||||
var b = data.get(pos++);
|
||||
switch (b) {
|
||||
case 'i'.code:
|
||||
return decodeInt();
|
||||
case 'l'.code:
|
||||
return decodeList();
|
||||
case 'd'.code:
|
||||
return decodeDict();
|
||||
default:
|
||||
return decodeBytes();
|
||||
}
|
||||
}
|
||||
|
||||
function decodeInt():Float {
|
||||
var start = pos;
|
||||
while (pos < data.length && data.get(pos) != 'e'.code) pos++;
|
||||
if (pos >= data.length) throw "Bencode: unterminated int";
|
||||
var s = data.getString(start, pos - start);
|
||||
pos++;
|
||||
var v = Std.parseFloat(s);
|
||||
if (Math.isNaN(v)) throw "Bencode: bad int " + s;
|
||||
return v;
|
||||
}
|
||||
|
||||
function decodeBytes():Bytes {
|
||||
var start = pos - 1;
|
||||
if (data.get(start) < '0'.code || data.get(start) > '9'.code) {
|
||||
throw "Bencode: bad byte length";
|
||||
}
|
||||
while (pos < data.length && data.get(pos) != ':'.code) pos++;
|
||||
if (pos >= data.length) throw "Bencode: unterminated length";
|
||||
var len = Std.parseInt(data.getString(start, pos - start));
|
||||
pos++;
|
||||
if (len == null || len < 0 || len > MAX_DECODE_LEN
|
||||
|| pos + len > data.length) {
|
||||
throw "Bencode: invalid byte length " + len;
|
||||
}
|
||||
var bytes = Bytes.alloc(len);
|
||||
bytes.blit(0, data, pos, len);
|
||||
pos += len;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function decodeList():Array<Dynamic> {
|
||||
var arr:Array<Dynamic> = [];
|
||||
while (true) {
|
||||
if (pos >= data.length) throw "Bencode: unterminated list";
|
||||
if (data.get(pos) == 'e'.code) break;
|
||||
arr.push(value());
|
||||
}
|
||||
pos++;
|
||||
return arr;
|
||||
}
|
||||
|
||||
function decodeDict():Dynamic {
|
||||
var obj:Dynamic = {};
|
||||
while (true) {
|
||||
if (pos >= data.length) throw "Bencode: unterminated dict";
|
||||
if (data.get(pos) == 'e'.code) break;
|
||||
var key:Bytes = value();
|
||||
Reflect.setField(obj, key.toString(), value());
|
||||
}
|
||||
pos++;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
152
leenkx/Sources/leenkx/network/torrent/Crypto.hx
Normal file
152
leenkx/Sources/leenkx/network/torrent/Crypto.hx
Normal file
@ -0,0 +1,152 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.Base58.Base58Check;
|
||||
|
||||
class Crypto {
|
||||
|
||||
public static var SEEDPREFIX(default, null) = "490a";
|
||||
public static var ADDRESSPREFIX(default, null) = "55";
|
||||
|
||||
static var drbg:Aegis128x4 = null;
|
||||
static var drbgBuf:Bytes = null;
|
||||
static var drbgPos:Int = 0;
|
||||
static var drbgNative:Int = -1;
|
||||
|
||||
public static function randomBytes(length:Int):Bytes {
|
||||
var bytes = Bytes.alloc(length);
|
||||
#if (js && !kha_krom)
|
||||
var webCrypto:Dynamic = js.Syntax.code("(typeof globalThis !== 'undefined' ? globalThis.crypto : null)");
|
||||
if (webCrypto != null && webCrypto.getRandomValues != null) {
|
||||
var view = new js.lib.Uint8Array(length);
|
||||
webCrypto.getRandomValues(view);
|
||||
for (i in 0...length) bytes.set(i, view[i]);
|
||||
return bytes;
|
||||
}
|
||||
#end
|
||||
#if lnx_datachannel
|
||||
if (drbgNative == -1) {
|
||||
var seed = osEntropy(32);
|
||||
try {
|
||||
drbgNative = datachannel.RTC.aegisAvailable()
|
||||
&& datachannel.RTC.aegisInit(seed.sub(0, 16), seed.sub(16, 16)) ? 1 : 0;
|
||||
} catch (e:Dynamic) {
|
||||
drbgNative = 0;
|
||||
}
|
||||
}
|
||||
if (drbgNative == 1) {
|
||||
return datachannel.RTC.aegisRandom(length);
|
||||
}
|
||||
#end
|
||||
if (drbg == null) {
|
||||
var seed = osEntropy(32);
|
||||
drbg = new Aegis128x4(seed.sub(0, 16), seed.sub(16, 16));
|
||||
drbgBuf = Bytes.alloc(0);
|
||||
drbgPos = 0;
|
||||
}
|
||||
var off = 0;
|
||||
while (off < length) {
|
||||
if (drbgPos >= drbgBuf.length) {
|
||||
drbgBuf = drbg.nextBlock();
|
||||
drbgPos = 0;
|
||||
}
|
||||
var n = Std.int(Math.min(drbgBuf.length - drbgPos, length - off));
|
||||
bytes.blit(off, drbgBuf, drbgPos, n);
|
||||
drbgPos += n;
|
||||
off += n;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static function osEntropy(length:Int):Bytes {
|
||||
var bytes = Bytes.alloc(length);
|
||||
#if kha_krom
|
||||
var kromBytes = leenkx.network.torrent.transport.RtcNative.randomBytes(length);
|
||||
if (kromBytes != null) return kromBytes;
|
||||
#end
|
||||
#if sys
|
||||
if (Sys.systemName() != "Windows") {
|
||||
try {
|
||||
var f = sys.io.File.read("/dev/urandom", true);
|
||||
f.readBytes(bytes, 0, length);
|
||||
f.close();
|
||||
return bytes;
|
||||
} catch(e:Dynamic) {}
|
||||
} else {
|
||||
try {
|
||||
var p = new sys.io.Process("powershell", [
|
||||
"-NoProfile", "-Command",
|
||||
"$r = New-Object 'byte[]' " + length + "; [Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($r); -join ($r | ForEach-Object { $_.ToString('x2') })"
|
||||
]);
|
||||
var hex = StringTools.trim(p.stdout.readAll().toString());
|
||||
p.close();
|
||||
if (hex.length == length * 2) {
|
||||
return Bytes.ofHex(hex);
|
||||
}
|
||||
} catch(e:Dynamic) {
|
||||
trace("Windows RNG via PowerShell failed: " + e);
|
||||
}
|
||||
}
|
||||
#end
|
||||
throw "Crypto: OS entropy unavailable. Refusing to generate predictable keys.";
|
||||
}
|
||||
|
||||
public static function hash(data:Bytes):Bytes {
|
||||
return TweetNaCl.hash(data);
|
||||
}
|
||||
|
||||
public static function signDetached(message:Bytes, secretKey:Bytes):Bytes {
|
||||
return TweetNaCl.signDetached(message, secretKey);
|
||||
}
|
||||
|
||||
public static function signDetachedVerify(message:Bytes, signature:Bytes, publicKey:Bytes):Bool {
|
||||
return TweetNaCl.signDetachedVerify(message, signature, publicKey);
|
||||
}
|
||||
|
||||
public static function box(message:Bytes, nonce:Bytes, theirPublicKey:Bytes, mySecretKey:Bytes):Bytes {
|
||||
return TweetNaCl.box(message, nonce, theirPublicKey, mySecretKey);
|
||||
}
|
||||
|
||||
public static function boxOpen(message:Bytes, nonce:Bytes, theirPublicKey:Bytes, mySecretKey:Bytes):Bytes {
|
||||
return TweetNaCl.boxOpen(message, nonce, theirPublicKey, mySecretKey);
|
||||
}
|
||||
|
||||
public static function boxKeyPair():{publicKey:Bytes, secretKey:Bytes} {
|
||||
return TweetNaCl.boxKeyPair();
|
||||
}
|
||||
|
||||
public static function signKeyPairFromSeed(seed:Bytes):{publicKey:Bytes, secretKey:Bytes} {
|
||||
return TweetNaCl.signKeyPairFromSeed(seed);
|
||||
}
|
||||
|
||||
public static var boxNonceLength(get, null):Int;
|
||||
static function get_boxNonceLength():Int {
|
||||
return TweetNaCl.crypto_box_NONCEBYTES;
|
||||
}
|
||||
|
||||
public static function ripemd160(data:Bytes):Bytes {
|
||||
return Ripemd160.make(data);
|
||||
}
|
||||
|
||||
public static function encodeSeed(material:Bytes):String {
|
||||
var prefix = Bytes.ofHex(SEEDPREFIX);
|
||||
var payload = Bytes.alloc(prefix.length + material.length);
|
||||
payload.blit(0, prefix, 0, prefix.length);
|
||||
payload.blit(prefix.length, material, 0, material.length);
|
||||
return Base58Check.encode(payload);
|
||||
}
|
||||
|
||||
public static function encodeAddress(publicKey:Bytes):String {
|
||||
var prefix = Bytes.ofHex(ADDRESSPREFIX);
|
||||
var hash = hash(publicKey);
|
||||
var ripemd = ripemd160(hash);
|
||||
var payload = Bytes.alloc(prefix.length + ripemd.length);
|
||||
payload.blit(0, prefix, 0, prefix.length);
|
||||
payload.blit(prefix.length, ripemd, 0, ripemd.length);
|
||||
return Base58Check.encode(payload);
|
||||
}
|
||||
|
||||
public static function toHex(bytes:Bytes):String {
|
||||
return bytes.toHex();
|
||||
}
|
||||
}
|
||||
21
leenkx/Sources/leenkx/network/torrent/LICENSE.md
Normal file
21
leenkx/Sources/leenkx/network/torrent/LICENSE.md
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Riccardo Mazzucco - All Rights reserved
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
580
leenkx/Sources/leenkx/network/torrent/LeenkxClient.hx
Normal file
580
leenkx/Sources/leenkx/network/torrent/LeenkxClient.hx
Normal file
@ -0,0 +1,580 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.Types;
|
||||
import leenkx.network.torrent.Base58.Base58Check;
|
||||
import leenkx.network.torrent.transport.ITransport;
|
||||
import leenkx.network.torrent.transport.ITransport.TransportOpts;
|
||||
import leenkx.network.torrent.TorrentInfo;
|
||||
|
||||
class LeenkxClient {
|
||||
|
||||
public static var PEERTIMEOUT:Float = 5 * 60 * 1000;
|
||||
public static var defaultHeartbeat:Int = 30000;
|
||||
|
||||
public var identifier:String;
|
||||
public var seed:String;
|
||||
public var pk:String;
|
||||
public var ek:String;
|
||||
public var timeout:Float;
|
||||
public var announce:Array<String>;
|
||||
public var infoHashBinary:String;
|
||||
public var peerIdBinary:String;
|
||||
|
||||
public var keyPair:{publicKey:Bytes, secretKey:Bytes};
|
||||
public var keyPairEncrypt:{publicKey:Bytes, secretKey:Bytes};
|
||||
|
||||
public var peers:Map<String, PeerInfo> = [];
|
||||
public var seen:Map<String, Float> = [];
|
||||
public var pkToAddress:Map<String, String> = [];
|
||||
public var isRelay:Bool = false;
|
||||
public var lastPeerCount:Null<Int> = null;
|
||||
public var lastPeerId:String = null;
|
||||
public var lastPeerIndex:Int = 0;
|
||||
|
||||
public var serverAddress:String = null;
|
||||
public var heartbeatTimer:Null<haxe.Timer> = null;
|
||||
|
||||
public var onOpen:Void -> Void;
|
||||
public var onTorrent:String -> Dynamic -> Void;
|
||||
public var onMessage:String -> Dynamic -> Dynamic -> Void;
|
||||
public var onSeen:String -> Void;
|
||||
public var onLeft:String -> Void;
|
||||
public var onServer:String -> Void;
|
||||
public var onPing:String -> Void;
|
||||
public var onTimeout:String -> Void;
|
||||
public var onConnections:Int -> Void;
|
||||
public var onWireSeen:Int -> Dynamic -> Void;
|
||||
public var onWireLeft:Int -> Dynamic -> Void;
|
||||
public var onTracker:String -> Dynamic -> Void;
|
||||
public var onAnnounce:String -> Void;
|
||||
public var onError:String -> Void;
|
||||
|
||||
public var transport:ITransport;
|
||||
private var deferredOpts:TransportOpts = null;
|
||||
private var heartbeatInterval:Int = 30000;
|
||||
|
||||
public var torrents(get, never):Map<String, Torrent>;
|
||||
public var onTorrentAdded:Torrent -> Void;
|
||||
public var onTorrentDone:Torrent -> Void;
|
||||
var swarm:TorrentClient = null;
|
||||
|
||||
function get_torrents():Map<String, Torrent> {
|
||||
return swarm != null ? swarm.torrents : [];
|
||||
}
|
||||
|
||||
function getSwarm():TorrentClient {
|
||||
if (swarm == null) {
|
||||
swarm = new TorrentClient();
|
||||
swarm.onError = function(e) {
|
||||
if (onError != null) onError(e);
|
||||
};
|
||||
swarm.onTorrentAdded = function(t) {
|
||||
if (onTorrentAdded != null) onTorrentAdded(t);
|
||||
};
|
||||
swarm.onTorrentDone = function(t) {
|
||||
if (onTorrentDone != null) onTorrentDone(t);
|
||||
};
|
||||
}
|
||||
return swarm;
|
||||
}
|
||||
|
||||
public function add(torrentId:Dynamic, ?opts:Dynamic, ?cb:Torrent -> Void):Torrent {
|
||||
return getSwarm().add(torrentId, opts, cb);
|
||||
}
|
||||
|
||||
public function seedMeta(meta:MetaInfo,
|
||||
files:Array<{path:String, data:Bytes}>,
|
||||
?opts:Dynamic, ?cb:Torrent -> Void):Torrent {
|
||||
return getSwarm().seedMeta(meta, files, opts, cb);
|
||||
}
|
||||
|
||||
public function get(id:Dynamic):Torrent {
|
||||
if (Std.isOfType(id, Torrent)) return id;
|
||||
return swarm != null ? swarm.get(id) : null;
|
||||
}
|
||||
|
||||
public function remove(infoHashHex:String, ?deleteData:Bool):Void {
|
||||
if (swarm != null) swarm.remove(infoHashHex, deleteData);
|
||||
}
|
||||
|
||||
public function listen(port:Int):Void {
|
||||
getSwarm().listen(port);
|
||||
}
|
||||
|
||||
public function new(?identifier:String, ?opts:LeenkxOptions) {
|
||||
if (identifier != null && !Std.isOfType(identifier, String)) {
|
||||
opts = cast identifier;
|
||||
identifier = null;
|
||||
}
|
||||
opts = opts != null ? opts : {};
|
||||
|
||||
this.announce = opts.announce != null ? opts.announce : TorrentClient.defaultTrackers;
|
||||
this.timeout = opts.timeout != null ? opts.timeout : PEERTIMEOUT;
|
||||
|
||||
if (opts.seed != null) {
|
||||
this.seed = opts.seed;
|
||||
} else {
|
||||
this.seed = Crypto.encodeSeed(Crypto.randomBytes(32));
|
||||
}
|
||||
|
||||
if (opts.keyPair != null) {
|
||||
this.keyPair = opts.keyPair;
|
||||
} else {
|
||||
var seedBytes = Base58Check.decode(this.seed);
|
||||
var material = seedBytes.sub(2, seedBytes.length - 2);
|
||||
this.keyPair = Crypto.signKeyPairFromSeed(material);
|
||||
}
|
||||
|
||||
this.keyPairEncrypt = Crypto.boxKeyPair();
|
||||
|
||||
this.pk = Base58.encode(this.keyPair.publicKey);
|
||||
this.ek = Base58.encode(this.keyPairEncrypt.publicKey);
|
||||
|
||||
this.identifier = identifier != null ? identifier : this.address();
|
||||
|
||||
var infoHash = TorrentInfo.identifierHash(this.identifier);
|
||||
this.infoHashBinary = TorrentInfo.toBinaryString(infoHash);
|
||||
this.peerIdBinary = TorrentInfo.toBinaryString(TorrentInfo.peerId());
|
||||
|
||||
createTransport(opts, opts.deferJoin == true);
|
||||
|
||||
this.heartbeatInterval = opts.heartbeat != null ? opts.heartbeat : defaultHeartbeat;
|
||||
}
|
||||
|
||||
function createTransport(opts:LeenkxOptions, defer:Bool):Void {
|
||||
var transportOpts:TransportOpts = {
|
||||
announce: this.announce,
|
||||
iceServers: opts.iceServers,
|
||||
tracker: opts.tracker,
|
||||
wtOpts: opts.wtOpts,
|
||||
torrentOpts: opts.torrentOpts, torrent: opts.torrent,
|
||||
pk: this.pk,
|
||||
ek: this.ek,
|
||||
infoHashBinary: this.infoHashBinary,
|
||||
peerIdBinary: this.peerIdBinary
|
||||
};
|
||||
|
||||
var announceUrl = this.announce[0];
|
||||
if (opts.transport == "datachannel") {
|
||||
transport = new leenkx.network.torrent.transport.DatachannelTransport(announceUrl, opts.iceServers);
|
||||
} else {
|
||||
transport = new leenkx.network.torrent.transport.WireTransport(announceUrl, opts.iceServers);
|
||||
}
|
||||
|
||||
transport.onReady = onTransportReady;
|
||||
transport.onPeerConnected = onTransportPeerConnected;
|
||||
transport.onPeerDisconnected = onTransportPeerDisconnected;
|
||||
transport.onPeerData = onTransportPeerData;
|
||||
transport.onPeerHandshake = onTransportPeerHandshake;
|
||||
transport.onTrackerUpdate = onTransportTrackerUpdate;
|
||||
transport.onTrackerAnnounce = onTransportTrackerAnnounce;
|
||||
transport.onError = onTransportError;
|
||||
|
||||
if (defer) {
|
||||
deferredOpts = transportOpts;
|
||||
} else {
|
||||
transport.join(this.identifier, transportOpts);
|
||||
}
|
||||
}
|
||||
|
||||
public function connect():Void {
|
||||
if (deferredOpts != null) {
|
||||
var opts = deferredOpts;
|
||||
deferredOpts = null;
|
||||
transport.join(this.identifier, opts);
|
||||
}
|
||||
this.heartbeat(this.heartbeatInterval);
|
||||
}
|
||||
|
||||
function onTransportReady():Void {
|
||||
if (onTorrent != null) onTorrent(identifier, null);
|
||||
}
|
||||
|
||||
function onTransportPeerConnected(peerId:String, count:Int):Void {
|
||||
lastPeerId = peerId;
|
||||
var prevCount = lastPeerCount;
|
||||
lastPeerCount = count;
|
||||
if (onWireSeen != null) onWireSeen(count, {peerId: peerId});
|
||||
if (count != prevCount) {
|
||||
if (onConnections != null) onConnections(count);
|
||||
}
|
||||
}
|
||||
|
||||
function onTransportPeerDisconnected(count:Int):Void {
|
||||
if (onWireLeft != null) onWireLeft(count, {peerId: lastPeerId});
|
||||
if (count != lastPeerCount) {
|
||||
lastPeerCount = count;
|
||||
if (onConnections != null) onConnections(count);
|
||||
}
|
||||
}
|
||||
|
||||
function onTransportPeerData(senderPk:String, data:Bytes):Void {
|
||||
onMessageData(identifier, data, senderPk);
|
||||
}
|
||||
|
||||
function onTransportPeerHandshake(peerPk:String, peerEk:String):Void {
|
||||
if (peerPk != null && peerPk != "") {
|
||||
sawPeer(peerPk, peerEk, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
function onTransportTrackerUpdate(update:Dynamic):Void {
|
||||
if (onTracker != null) onTracker(identifier, update);
|
||||
}
|
||||
|
||||
function onTransportTrackerAnnounce():Void {
|
||||
if (onAnnounce != null) onAnnounce(identifier);
|
||||
}
|
||||
|
||||
function onTransportError(msg:String):Void {
|
||||
trace("onTransportError: " + msg);
|
||||
if (onError != null) onError(msg);
|
||||
}
|
||||
|
||||
public function address(?pk:Dynamic):String {
|
||||
var pkBytes:Bytes = null;
|
||||
if (pk != null && Std.isOfType(pk, String)) {
|
||||
pkBytes = Base58.decode(pk);
|
||||
} else if (pk != null) {
|
||||
pkBytes = pk;
|
||||
} else {
|
||||
pkBytes = this.keyPair.publicKey;
|
||||
}
|
||||
return Crypto.encodeAddress(pkBytes);
|
||||
}
|
||||
|
||||
function addressFromPk(pkStr:String):String {
|
||||
var cached = pkToAddress.get(pkStr);
|
||||
if (cached != null) return cached;
|
||||
var addr = address(Base58.decode(pkStr));
|
||||
pkToAddress.set(pkStr, addr);
|
||||
return addr;
|
||||
}
|
||||
|
||||
public function encodeSeed(material:Bytes):String {
|
||||
return Crypto.encodeSeed(material);
|
||||
}
|
||||
|
||||
public function ping():Void {
|
||||
var packet = makePacket({y: PacketType.Ping});
|
||||
sendRaw(packet);
|
||||
}
|
||||
|
||||
public function send(?address:String, message:Dynamic):Void {
|
||||
if (message == null) {
|
||||
message = address;
|
||||
address = null;
|
||||
}
|
||||
var packet = makePacket({
|
||||
y: PacketType.Message,
|
||||
v: haxe.Json.stringify(message)
|
||||
});
|
||||
var peerPk:String = null;
|
||||
if (address != null) {
|
||||
if (peers.exists(address)) {
|
||||
peerPk = peers[address].pk;
|
||||
packet = encryptPacket(peerPk, packet);
|
||||
} else {
|
||||
throw address + " not seen - no public key.";
|
||||
}
|
||||
}
|
||||
sendRaw(packet, peerPk);
|
||||
}
|
||||
|
||||
public function sendRawMessage(data:Bytes, ?address:String):Void {
|
||||
var marked = Bytes.alloc(data.length + 1);
|
||||
marked.set(0, 0x00);
|
||||
marked.blit(1, data, 0, data.length);
|
||||
if (transport != null) {
|
||||
if (address != null) {
|
||||
if (!peers.exists(address)) {
|
||||
throw address + " not seen - no public key.";
|
||||
}
|
||||
if (!transport.sendTo(peers[address].pk, marked)) {
|
||||
trace("sendRawMessage: no open channel to " + address);
|
||||
}
|
||||
} else {
|
||||
transport.broadcast(marked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function heartbeat(?interval:Int):Void {
|
||||
interval = interval != null ? interval : 30000;
|
||||
if (heartbeatTimer != null) heartbeatTimer.stop();
|
||||
heartbeatTimer = new haxe.Timer(interval);
|
||||
heartbeatTimer.run = () -> {
|
||||
ping();
|
||||
var t = now();
|
||||
for (p in [for (k in peers.keys()) k]) {
|
||||
var peerInfo = peers[p];
|
||||
if (peerInfo.last + interval < t) {
|
||||
peers.remove(p);
|
||||
if (onTimeout != null) onTimeout(p);
|
||||
if (onLeft != null) onLeft(p);
|
||||
}
|
||||
}
|
||||
for (h in [for (k in seen.keys()) k]) {
|
||||
if (seen.get(h) + interval < t) {
|
||||
seen.remove(h);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function destroy(?cb:Void -> Void):Void {
|
||||
if (heartbeatTimer != null) {
|
||||
heartbeatTimer.stop();
|
||||
heartbeatTimer = null;
|
||||
}
|
||||
var packet = makePacket({y: PacketType.Leave});
|
||||
sendRaw(packet);
|
||||
if (swarm != null) {
|
||||
swarm.destroy();
|
||||
swarm = null;
|
||||
}
|
||||
if (transport != null) {
|
||||
transport.destroy(cb);
|
||||
} else if (cb != null) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
public function close(?cb:Void -> Void):Void {
|
||||
destroy(cb);
|
||||
}
|
||||
|
||||
public function connections():Int {
|
||||
if (transport != null) {
|
||||
var count = transport.getPeerCount();
|
||||
if (count != lastPeerCount) {
|
||||
lastPeerCount = count;
|
||||
if (onConnections != null) onConnections(count);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function makePacket(params:Dynamic):Bytes {
|
||||
var p:Dynamic = {
|
||||
t: now(),
|
||||
i: identifier,
|
||||
pk: pk,
|
||||
ek: ek,
|
||||
n: Crypto.randomBytes(8)
|
||||
};
|
||||
for (k in Reflect.fields(params)) {
|
||||
Reflect.setField(p, k, Reflect.field(params, k));
|
||||
}
|
||||
var pe = Bencode.encode(p);
|
||||
var sig = Crypto.signDetached(pe, keyPair.secretKey);
|
||||
return Bencode.encode({s: sig, p: pe});
|
||||
}
|
||||
|
||||
function encryptPacket(peerPk:String, packet:Bytes):Bytes {
|
||||
var addr = addressFromPk(peerPk);
|
||||
if (peers.exists(addr)) {
|
||||
var nonce = Crypto.randomBytes(Crypto.boxNonceLength);
|
||||
var encrypted = Crypto.box(
|
||||
packet,
|
||||
nonce,
|
||||
Base58.decode(peers[addr].ek),
|
||||
keyPairEncrypt.secretKey
|
||||
);
|
||||
return Bencode.encode({
|
||||
n: nonce,
|
||||
ek: Base58.encode(keyPairEncrypt.publicKey),
|
||||
e: encrypted
|
||||
});
|
||||
} else {
|
||||
throw addressFromPk(peerPk) + " not seen - no encryption key.";
|
||||
}
|
||||
}
|
||||
|
||||
function sendRaw(message:Bytes, ?peerPk:String):Void {
|
||||
var hash = Crypto.hash(message).sub(0, 16).toHex();
|
||||
seen.set(hash, now());
|
||||
if (transport != null) {
|
||||
if (peerPk != null) {
|
||||
if (!transport.sendTo(peerPk, message)) {
|
||||
transport.broadcast(message);
|
||||
}
|
||||
} else {
|
||||
transport.broadcast(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onMessageData(identifier:String, message:Bytes, ?senderPk:String):Void {
|
||||
var hash = (message.length > 0 && message.get(0) == 0x00)
|
||||
? null : Crypto.hash(message).sub(0, 16).toHex();
|
||||
var t = now();
|
||||
if (hash == null || !seen.exists(hash)) {
|
||||
if (message.length > 0 && (message.get(0) == 0x00 || message.get(0) == 0x01)) {
|
||||
var isRelayed = message.get(0) == 0x01;
|
||||
var offset = 1;
|
||||
var msgSenderPk:String = senderPk;
|
||||
if (isRelayed) {
|
||||
if (message.length < 2) {
|
||||
seen.set(hash, t);
|
||||
return;
|
||||
}
|
||||
var pkLen = message.get(1);
|
||||
if (2 + pkLen > message.length) {
|
||||
seen.set(hash, t);
|
||||
return;
|
||||
}
|
||||
var claimedPk = message.sub(2, pkLen).toString();
|
||||
if (pkToAddress.exists(claimedPk)) {
|
||||
msgSenderPk = claimedPk;
|
||||
}
|
||||
offset = 2 + pkLen;
|
||||
}
|
||||
var rawPayload = message.sub(offset, message.length - offset);
|
||||
var payload = new leenkx.network.Buffer();
|
||||
payload.writeBytes(rawPayload);
|
||||
if (isRelayed) seen.set(hash, t);
|
||||
var senderAddr = msgSenderPk != null ? pkToAddress.get(msgSenderPk) : null;
|
||||
if (senderAddr == null && msgSenderPk != null && msgSenderPk != "") {
|
||||
senderAddr = addressFromPk(msgSenderPk);
|
||||
}
|
||||
if (onMessage != null) {
|
||||
onMessage(senderAddr, payload, null);
|
||||
}
|
||||
if (isRelay && transport != null && transport.getPeerCount() > 1) {
|
||||
if (isRelayed) {
|
||||
sendRaw(message);
|
||||
} else if (msgSenderPk != null && msgSenderPk != "") {
|
||||
var pkBytes = Bytes.ofString(msgSenderPk);
|
||||
var relayed = Bytes.alloc(1 + 1 + pkBytes.length + rawPayload.length);
|
||||
relayed.set(0, 0x01);
|
||||
relayed.set(1, pkBytes.length);
|
||||
relayed.blit(2, pkBytes, 0, pkBytes.length);
|
||||
relayed.blit(2 + pkBytes.length, rawPayload, 0, rawPayload.length);
|
||||
sendRaw(relayed);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var unpacked:Dynamic = null;
|
||||
try {
|
||||
unpacked = Bencode.decode(message);
|
||||
} catch(e:Dynamic) {
|
||||
trace("Bencode decode error: " + e);
|
||||
seen.set(hash, t);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
var isEncrypted = Reflect.field(unpacked, "e") != null
|
||||
&& Reflect.field(unpacked, "n") != null
|
||||
&& Reflect.field(unpacked, "ek") != null;
|
||||
|
||||
if (isEncrypted) {
|
||||
var ekStr:String = unpacked.ek.toString();
|
||||
var decrypted = Crypto.boxOpen(
|
||||
unpacked.e,
|
||||
unpacked.n,
|
||||
Base58.decode(ekStr),
|
||||
keyPairEncrypt.secretKey
|
||||
);
|
||||
if (decrypted != null) {
|
||||
unpacked = Bencode.decode(decrypted);
|
||||
} else {
|
||||
unpacked = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (unpacked != null && unpacked.p != null) {
|
||||
var packet:Dynamic = Bencode.decode(unpacked.p);
|
||||
var pkStr:String = packet.pk.toString();
|
||||
var idStr:String = packet.i.toString();
|
||||
var checkSig = Crypto.signDetachedVerify(unpacked.p, unpacked.s, Base58.decode(pkStr));
|
||||
var checkId = idStr == identifier;
|
||||
var checkTime = packet.t + timeout > t;
|
||||
|
||||
if (checkSig && checkId && checkTime) {
|
||||
var ekStr:String = packet.ek.toString();
|
||||
sawPeer(pkStr, ekStr, identifier);
|
||||
|
||||
var yStr:String = packet.y != null ? packet.y.toString() : null;
|
||||
if (yStr == PacketType.Message) {
|
||||
var msgStr:String = packet.v.toString();
|
||||
var msgJson:Dynamic = null;
|
||||
try {
|
||||
msgJson = haxe.Json.parse(msgStr);
|
||||
} catch(e) {
|
||||
}
|
||||
if (msgJson != null) {
|
||||
if (onMessage != null) {
|
||||
onMessage(addressFromPk(pkStr), msgJson, packet);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
var payload = new leenkx.network.Buffer();
|
||||
payload.writeBytes(
|
||||
haxe.crypto.Base64.decode(msgStr));
|
||||
if (onMessage != null) {
|
||||
onMessage(addressFromPk(pkStr), payload, packet);
|
||||
}
|
||||
} catch(e:Dynamic) {}
|
||||
}
|
||||
} else if (yStr == PacketType.Ping) {
|
||||
if (onPing != null) onPing(addressFromPk(pkStr));
|
||||
} else if (yStr == PacketType.Leave) {
|
||||
var addr = addressFromPk(pkStr);
|
||||
peers.remove(addr);
|
||||
if (onLeft != null) onLeft(addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (transport != null && transport.getPeerCount() > 1) {
|
||||
sendRaw(message);
|
||||
}
|
||||
} catch(e:Dynamic) {
|
||||
trace("onMessageData error processing packet: " + e);
|
||||
}
|
||||
}
|
||||
seen.set(hash, t);
|
||||
}
|
||||
|
||||
function sawPeer(pk:String, ek:String, identifier:String):Void {
|
||||
var t = now();
|
||||
var addr = addressFromPk(pk);
|
||||
if (addr == address()) return;
|
||||
if (!peers.exists(addr) || peers[addr].last + timeout < t) {
|
||||
var index = 0;
|
||||
var taken = true;
|
||||
while (taken) {
|
||||
taken = false;
|
||||
index++;
|
||||
for (k in peers.keys()) {
|
||||
if (peers[k].index == index) { taken = true; break; }
|
||||
}
|
||||
}
|
||||
peers.set(addr, {pk: pk, ek: ek, last: t, index: index});
|
||||
lastPeerIndex = index;
|
||||
lastPeerId = addr;
|
||||
if (onOpen != null) onOpen();
|
||||
if (onSeen != null) onSeen(addr);
|
||||
if (addr == identifier) {
|
||||
serverAddress = addr;
|
||||
if (onServer != null) onServer(addr);
|
||||
}
|
||||
var packet = makePacket({y: PacketType.Ping});
|
||||
sendRaw(packet);
|
||||
} else {
|
||||
var p = peers.get(addr);
|
||||
p.ek = ek;
|
||||
p.last = t;
|
||||
}
|
||||
}
|
||||
|
||||
static function now():Float {
|
||||
return Date.now().getTime();
|
||||
}
|
||||
}
|
||||
115
leenkx/Sources/leenkx/network/torrent/Magnet.hx
Normal file
115
leenkx/Sources/leenkx/network/torrent/Magnet.hx
Normal file
@ -0,0 +1,115 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class Magnet {
|
||||
public var infoHash:Bytes;
|
||||
public var infoHashHex:String;
|
||||
public var infoHashV2:Bytes;
|
||||
public var infoHashV2Hex:String;
|
||||
public var name:String = null;
|
||||
public var trackers:Array<String> = [];
|
||||
public var webSeeds:Array<String> = [];
|
||||
|
||||
public function new() {}
|
||||
|
||||
public static function parse(uri:String):Magnet {
|
||||
if (uri.indexOf("magnet:?") != 0) return null;
|
||||
var m = new Magnet();
|
||||
var query = uri.substr(8);
|
||||
for (param in query.split("&")) {
|
||||
var eq = param.indexOf("=");
|
||||
if (eq < 0) continue;
|
||||
var key = param.substr(0, eq);
|
||||
var value = StringTools.urlDecode(param.substr(eq + 1));
|
||||
switch (key) {
|
||||
case "xt":
|
||||
if (value.indexOf("urn:btih:") == 0) {
|
||||
var hash = value.substr(9);
|
||||
m.infoHash = decodeBtih(hash);
|
||||
if (m.infoHash != null) {
|
||||
m.infoHashHex = Crypto.toHex(m.infoHash);
|
||||
}
|
||||
} else if (value.indexOf("urn:btmh:") == 0) {
|
||||
var hash = value.substr(9);
|
||||
if (hash.length == 68) hash = hash.substr(4);
|
||||
if (hash.length == 64) {
|
||||
try {
|
||||
m.infoHashV2 = Bytes.ofHex(hash);
|
||||
} catch(e:Dynamic) {}
|
||||
if (m.infoHashV2 != null) {
|
||||
m.infoHashV2Hex =
|
||||
Crypto.toHex(m.infoHashV2);
|
||||
}
|
||||
}
|
||||
}
|
||||
case "dn":
|
||||
m.name = value;
|
||||
case "tr":
|
||||
m.trackers.push(value);
|
||||
case "ws":
|
||||
m.webSeeds.push(value);
|
||||
default:
|
||||
}
|
||||
}
|
||||
if (m.infoHash == null && m.infoHashV2 == null) return null;
|
||||
return m;
|
||||
}
|
||||
|
||||
public static function build(infoHash:Bytes, name:String, trackers:Array<String>, ?webSeeds:Array<String>, ?infoHashV2:Bytes):String {
|
||||
var parts:Array<String> = [];
|
||||
if (infoHash != null) {
|
||||
parts.push("xt=urn:btih:" + Crypto.toHex(infoHash));
|
||||
}
|
||||
if (infoHashV2 != null) {
|
||||
parts.push("xt=urn:btmh:1220"
|
||||
+ Crypto.toHex(infoHashV2));
|
||||
}
|
||||
if (name != null) {
|
||||
parts.push("dn=" + StringTools.urlEncode(name));
|
||||
}
|
||||
if (trackers != null) {
|
||||
for (t in trackers) {
|
||||
parts.push("tr=" + StringTools.urlEncode(t));
|
||||
}
|
||||
}
|
||||
if (webSeeds != null) {
|
||||
for (w in webSeeds) {
|
||||
parts.push("ws=" + StringTools.urlEncode(w));
|
||||
}
|
||||
}
|
||||
return "magnet:?" + parts.join("&");
|
||||
}
|
||||
|
||||
static function decodeBtih(s:String):Bytes {
|
||||
if (s.length == 40) {
|
||||
try {
|
||||
return Bytes.ofHex(s);
|
||||
} catch(e:Dynamic) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (s.length == 32) {
|
||||
return base32Decode(s.toUpperCase());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function base32Decode(s:String):Bytes {
|
||||
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
var out = new haxe.io.BytesBuffer();
|
||||
var buffer = 0;
|
||||
var bits = 0;
|
||||
for (i in 0...s.length) {
|
||||
var v = alphabet.indexOf(s.charAt(i));
|
||||
if (v < 0) return null;
|
||||
buffer = (buffer << 5) | v;
|
||||
bits += 5;
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
out.addByte((buffer >> bits) & 0xFF);
|
||||
}
|
||||
}
|
||||
return out.getBytes();
|
||||
}
|
||||
}
|
||||
711
leenkx/Sources/leenkx/network/torrent/MetaInfo.hx
Normal file
711
leenkx/Sources/leenkx/network/torrent/MetaInfo.hx
Normal file
@ -0,0 +1,711 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
typedef TorrentFileEntry = {
|
||||
var path:String;
|
||||
var length:Int;
|
||||
var offset:Int;
|
||||
@:optional var piecesRoot:Bytes;
|
||||
}
|
||||
|
||||
class MetaInfo {
|
||||
public var infoHash:Bytes;
|
||||
public var infoHashHex:String;
|
||||
public var infoHashV2:Bytes = null;
|
||||
public var infoHashV2Hex:String = null;
|
||||
public var metaVersion:Int = 1;
|
||||
public var name:String;
|
||||
public var pieceLength:Int;
|
||||
public var pieces:Bytes;
|
||||
public var files:Array<TorrentFileEntry>;
|
||||
public var totalLength:Int = 0;
|
||||
public var announce:Array<String>;
|
||||
public var rawInfo:Bytes;
|
||||
public var isPrivate:Bool = false;
|
||||
public var comment:String = null;
|
||||
public var createdBy:String = null;
|
||||
public var creationDate:Int = 0;
|
||||
public var urlList:Array<String> = null;
|
||||
public var source:String = null;
|
||||
public var fileHashes:Array<Array<Bytes>> = null;
|
||||
public var storageLength:Int = 0;
|
||||
public var pieceLayers:Array<{root:Bytes, hashes:Bytes}> = null;
|
||||
|
||||
public var numPieces(get, never):Int;
|
||||
function get_numPieces():Int {
|
||||
if (pieces != null && pieces.length > 0) {
|
||||
return Std.int(pieces.length / TorrentInfo.HASH_LENGTH);
|
||||
}
|
||||
if (pieceLength > 0 && files != null) {
|
||||
var end = 0;
|
||||
for (f in files) {
|
||||
var e = f.offset + f.length;
|
||||
if (e > end) end = e;
|
||||
}
|
||||
return end > 0
|
||||
? Std.int(Math.ceil(end / pieceLength)) : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public var protocolVersion(get, never):Int;
|
||||
function get_protocolVersion():Int {
|
||||
return metaVersion == 2 ? (pieces != null ? 3 : 2) : 1;
|
||||
}
|
||||
|
||||
public function new() {}
|
||||
|
||||
public static function fromTorrentBytes(data:Bytes):MetaInfo {
|
||||
var dict:Dynamic = Bencode.decode(data);
|
||||
if (dict == null || Reflect.field(dict, "info") == null) {
|
||||
throw "MetaInfo: no info dict";
|
||||
}
|
||||
var m = new MetaInfo();
|
||||
m.rawInfo = extractInfoBytes(data);
|
||||
var info = Reflect.field(dict, "info");
|
||||
m.parseInfo(info);
|
||||
if (m.metaVersion == 2) {
|
||||
m.infoHashV2 = haxe.crypto.Sha256.make(m.rawInfo);
|
||||
m.infoHashV2Hex = Crypto.toHex(m.infoHashV2);
|
||||
}
|
||||
if (m.pieces != null) {
|
||||
m.infoHash = haxe.crypto.Sha1.make(m.rawInfo);
|
||||
m.infoHashHex = Crypto.toHex(m.infoHash);
|
||||
}
|
||||
m.announce = parseAnnounce(dict);
|
||||
m.initFileHashes();
|
||||
m.loadPieceLayers(data);
|
||||
m.comment = bytesToString(Reflect.field(dict, "comment"));
|
||||
m.createdBy = bytesToString(Reflect.field(dict, "created by"));
|
||||
m.creationDate = Std.int(fieldFloat(dict, "creation date"));
|
||||
var ul = Reflect.field(dict, "url-list");
|
||||
if (Std.isOfType(ul, Array)) {
|
||||
m.urlList = [for (u in (ul:Array<Dynamic>))
|
||||
bytesToString(u)];
|
||||
} else if (ul != null) {
|
||||
m.urlList = [bytesToString(ul)];
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
function parseInfo(info:Dynamic):Void {
|
||||
name = bytesToString(Reflect.field(info, "name"));
|
||||
if (name == null) name = "";
|
||||
pieceLength = Std.int(fieldFloat(info, "piece length"));
|
||||
metaVersion = Std.int(fieldFloat(info, "meta version"));
|
||||
if (metaVersion == 0) metaVersion = 1;
|
||||
var piecesField = Reflect.field(info, "pieces");
|
||||
if (piecesField == null) {
|
||||
pieces = null;
|
||||
} else {
|
||||
pieces = Std.isOfType(piecesField, Bytes) ? piecesField
|
||||
: Bytes.ofString(Std.string(piecesField));
|
||||
}
|
||||
isPrivate = fieldFloat(info, "private") == 1;
|
||||
source = bytesToString(Reflect.field(info, "source"));
|
||||
|
||||
files = [];
|
||||
totalLength = 0;
|
||||
var fileTree = Reflect.field(info, "file tree");
|
||||
if (fileTree != null) {
|
||||
walkFileTree(fileTree, [name]);
|
||||
if (pieces != null) {
|
||||
applyV1Layout(Reflect.field(info, "files"));
|
||||
}
|
||||
storageLength = pieces == null
|
||||
? treeCursor * pieceLength : totalLength;
|
||||
return;
|
||||
}
|
||||
storageLength = 0;
|
||||
var filesList:Array<Dynamic> = Reflect.field(info, "files");
|
||||
if (filesList != null) {
|
||||
for (f in filesList) {
|
||||
var len = Std.int(fieldFloat(f, "length"));
|
||||
var pathList:Array<Dynamic> = Reflect.field(f, "path");
|
||||
var path = name;
|
||||
if (pathList != null) {
|
||||
var parts = [for (p in pathList) bytesToString(p)];
|
||||
path = name + "/" + parts.join("/");
|
||||
}
|
||||
files.push({path: sanitizePath(path), length: len,
|
||||
offset: totalLength});
|
||||
totalLength += len;
|
||||
}
|
||||
} else {
|
||||
var len = Std.int(fieldFloat(info, "length"));
|
||||
files.push({path: sanitizePath(name), length: len,
|
||||
offset: 0});
|
||||
totalLength = len;
|
||||
}
|
||||
storageLength = totalLength;
|
||||
}
|
||||
|
||||
public static function sanitizeComponent(c:String):String {
|
||||
if (c == null || c == "" || c == "." || c == ".."
|
||||
|| c.indexOf("/") >= 0 || c.indexOf("\\") >= 0
|
||||
|| c.indexOf(":") >= 0
|
||||
|| c.indexOf(String.fromCharCode(0)) >= 0) {
|
||||
return "_";
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public static function sanitizePath(p:String):String {
|
||||
if (p == null) return "_";
|
||||
var parts = p.split("/");
|
||||
return [for (c in parts) sanitizeComponent(c)].join("/");
|
||||
}
|
||||
|
||||
public function fileForPiece(index:Int):Int {
|
||||
for (i in 0...files.length) {
|
||||
var f = files[i];
|
||||
var start = Std.int(f.offset / pieceLength);
|
||||
var np = Std.int(Math.ceil(f.length / pieceLength));
|
||||
if (index >= start && index < start + np) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public function pieceSizeV2(index:Int):Int {
|
||||
var fi = fileForPiece(index);
|
||||
if (fi < 0) return 0;
|
||||
var f = files[fi];
|
||||
var start = Std.int(f.offset / pieceLength);
|
||||
var rem = f.length - (index - start) * pieceLength;
|
||||
return Std.int(Math.min(pieceLength, rem));
|
||||
}
|
||||
|
||||
public function verifyPieceV2(index:Int, data:Bytes):Bool {
|
||||
var fi = fileForPiece(index);
|
||||
if (fi < 0 || fileHashes == null
|
||||
|| fileHashes.length <= fi || fileHashes[fi] == null) {
|
||||
return false;
|
||||
}
|
||||
var f = files[fi];
|
||||
var start = Std.int(f.offset / pieceLength);
|
||||
var expected = fileHashes[fi][index - start];
|
||||
if (expected == null) return false;
|
||||
var actual = f.length <= pieceLength
|
||||
? merkleRoot(data) : pieceRootV2(data);
|
||||
return memcmp(actual, expected);
|
||||
}
|
||||
|
||||
public function pieceRootV2(data:Bytes):Bytes {
|
||||
var base = Std.int(Math.log(pieceLength / 16384)
|
||||
/ Math.log(2));
|
||||
var pieceLeaves = 1 << base;
|
||||
var leaves:Array<Bytes> = [];
|
||||
var pos = 0;
|
||||
while (pos < data.length) {
|
||||
var n = Std.int(Math.min(16384, data.length - pos));
|
||||
leaves.push(haxe.crypto.Sha256.make(data.sub(pos, n)));
|
||||
pos += n;
|
||||
}
|
||||
var zero = leafZero();
|
||||
while (leaves.length < pieceLeaves) leaves.push(zero);
|
||||
return merkleReduce(leaves);
|
||||
}
|
||||
|
||||
public function verifyLayer(fi:Int, hashes:Array<Bytes>):Bool {
|
||||
if (fi < 0 || hashes == null) return false;
|
||||
var root = files[fi].piecesRoot;
|
||||
if (root == null) return false;
|
||||
var base = Std.int(Math.log(pieceLength / 16384)
|
||||
/ Math.log(2));
|
||||
var level = hashes.copy();
|
||||
var pow = 1;
|
||||
while (pow < level.length) pow <<= 1;
|
||||
var zero = layerZero(base);
|
||||
while (level.length < pow) level.push(zero);
|
||||
return memcmp(merkleReduce(level), root);
|
||||
}
|
||||
|
||||
static var layerZeroCache:Map<Int, Bytes> = [];
|
||||
public static function layerZero(level:Int):Bytes {
|
||||
if (level == 0) return leafZero();
|
||||
var half = layerZero(level - 1);
|
||||
return hashPair(half, half);
|
||||
}
|
||||
|
||||
public static function hashPair(a:Bytes, b:Bytes):Bytes {
|
||||
var buf = new haxe.io.BytesBuffer();
|
||||
buf.addBytes(a, 0, 32);
|
||||
buf.addBytes(b, 0, 32);
|
||||
return haxe.crypto.Sha256.make(buf.getBytes());
|
||||
}
|
||||
|
||||
public static function merkleReduce(hashes:Array<Bytes>):Bytes {
|
||||
var level = hashes;
|
||||
while (level.length > 1) {
|
||||
var next:Array<Bytes> = [];
|
||||
for (i in 0...Std.int(level.length / 2)) {
|
||||
next.push(hashPair(level[i * 2], level[i * 2 + 1]));
|
||||
}
|
||||
level = next;
|
||||
}
|
||||
return level.length > 0 ? level[0] : Bytes.alloc(32);
|
||||
}
|
||||
|
||||
static public function memcmp(a:Bytes, b:Bytes):Bool {
|
||||
if (a == null || b == null || a.length != b.length) return false;
|
||||
for (i in 0...a.length) if (a.get(i) != b.get(i)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
var treeCursor:Int = 0;
|
||||
function walkFileTree(node:Dynamic, path:Array<String>):Void {
|
||||
var aligned = pieces == null;
|
||||
var keys = Reflect.fields(node);
|
||||
keys.sort(Reflect.compare);
|
||||
for (key in keys) {
|
||||
var child = Reflect.field(node, key);
|
||||
if (key == "") {
|
||||
var len = Std.int(fieldFloat(child, "length"));
|
||||
var p = path.length == 2 && path[0] == name
|
||||
&& path[1] == name ? name : path.join("/");
|
||||
var offset = aligned ? treeCursor * pieceLength
|
||||
: totalLength;
|
||||
files.push({path: sanitizePath(p), length: len,
|
||||
offset: offset,
|
||||
piecesRoot: toBytes(
|
||||
Reflect.field(child, "pieces root"))});
|
||||
if (aligned) {
|
||||
treeCursor += Std.int(
|
||||
Math.ceil(len / pieceLength));
|
||||
}
|
||||
totalLength += len;
|
||||
} else {
|
||||
var next = path.copy();
|
||||
next.push(key);
|
||||
walkFileTree(child, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyV1Layout(filesList:Dynamic):Void {
|
||||
if (!Std.isOfType(filesList, Array)) return;
|
||||
var byPath = new Map<String, TorrentFileEntry>();
|
||||
for (f in files) byPath.set(f.path, f);
|
||||
var ordered:Array<TorrentFileEntry> = [];
|
||||
var off = 0;
|
||||
for (fl in (filesList:Array<Dynamic>)) {
|
||||
var len = Std.int(fieldFloat(fl, "length"));
|
||||
var pathList:Array<Dynamic> = Reflect.field(fl, "path");
|
||||
var path = name;
|
||||
if (pathList != null) {
|
||||
var parts = [for (p in pathList) bytesToString(p)];
|
||||
path = name + "/" + parts.join("/");
|
||||
}
|
||||
var e = byPath.get(sanitizePath(path));
|
||||
if (e == null) {
|
||||
e = {path: sanitizePath(path), length: len,
|
||||
offset: off};
|
||||
} else {
|
||||
e.offset = off;
|
||||
e.length = len;
|
||||
}
|
||||
off += len;
|
||||
ordered.push(e);
|
||||
}
|
||||
files = ordered;
|
||||
totalLength = off;
|
||||
}
|
||||
|
||||
public function initFileHashes():Void {
|
||||
if (metaVersion != 2 || files == null
|
||||
|| pieceLength <= 0) return;
|
||||
if (fileHashes == null) {
|
||||
fileHashes = [for (f in files) null];
|
||||
}
|
||||
for (fi in 0...files.length) {
|
||||
var f = files[fi];
|
||||
if (f.length > 0 && f.length <= pieceLength
|
||||
&& f.piecesRoot != null) {
|
||||
fileHashes[fi] = [f.piecesRoot];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function loadPieceLayers(data:Bytes):Void {
|
||||
if (metaVersion != 2 || fileHashes == null) return;
|
||||
var pos = findTopKey(data, "piece layers");
|
||||
if (pos < 0 || data.get(pos) != 'd'.code) return;
|
||||
var byRoot = new Map<String, Bytes>();
|
||||
var p = pos + 1;
|
||||
while (p < data.length && data.get(p) != 'e'.code) {
|
||||
var key = rawString(data, p);
|
||||
if (key == null) return;
|
||||
p = skipValue(data, p);
|
||||
var val = rawString(data, p);
|
||||
if (val == null) return;
|
||||
p = skipValue(data, p);
|
||||
byRoot.set(Crypto.toHex(key), val);
|
||||
}
|
||||
for (fi in 0...files.length) {
|
||||
var f = files[fi];
|
||||
if (f.piecesRoot == null) continue;
|
||||
var np = Std.int(Math.ceil(f.length / pieceLength));
|
||||
if (np <= 1) continue;
|
||||
var raw = byRoot.get(Crypto.toHex(f.piecesRoot));
|
||||
if (raw == null || raw.length < np * 32) continue;
|
||||
var hashes = [for (i in 0...np) raw.sub(i * 32, 32)];
|
||||
if (verifyLayer(fi, hashes)) fileHashes[fi] = hashes;
|
||||
}
|
||||
}
|
||||
|
||||
static function rawString(data:Bytes, pos:Int):Bytes {
|
||||
var b = data.get(pos);
|
||||
if (b < '0'.code || b > '9'.code) return null;
|
||||
var len = 0;
|
||||
while (pos < data.length && data.get(pos) != ':'.code) {
|
||||
len = len * 10 + (data.get(pos) - '0'.code);
|
||||
pos++;
|
||||
}
|
||||
if (pos + 1 + len > data.length) return null;
|
||||
return data.sub(pos + 1, len);
|
||||
}
|
||||
|
||||
static function findTopKey(data:Bytes, want:String):Int {
|
||||
if (data.length == 0 || data.get(0) != 'd'.code) return -1;
|
||||
var pos = 1;
|
||||
while (pos < data.length && data.get(pos) != 'e'.code) {
|
||||
var keyEnd = skipValue(data, pos);
|
||||
var key = data.sub(pos, keyEnd - pos).toString();
|
||||
var colon = key.indexOf(":");
|
||||
key = key.substr(colon + 1);
|
||||
var valEnd = skipValue(data, keyEnd);
|
||||
if (key == want) return keyEnd;
|
||||
pos = valEnd;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static function parseAnnounce(dict:Dynamic):Array<String> {
|
||||
var out:Array<String> = [];
|
||||
var seen = new Map<String, Bool>();
|
||||
var list:Array<Dynamic> = Reflect.field(dict, "announce-list");
|
||||
if (list != null) {
|
||||
for (tier in list) {
|
||||
if (Std.isOfType(tier, Array)) {
|
||||
for (u in (tier:Array<Dynamic>)) {
|
||||
var s = bytesToString(u);
|
||||
if (s != null && !seen.exists(s)) {
|
||||
seen.set(s, true);
|
||||
out.push(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var single = bytesToString(Reflect.field(dict, "announce"));
|
||||
if (single != null && !seen.exists(single)) {
|
||||
out.push(single);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public static function fromInfoDict(rawInfo:Bytes, infoHash:Bytes, ?announce:Array<String>):MetaInfo {
|
||||
var m = new MetaInfo();
|
||||
m.rawInfo = rawInfo;
|
||||
m.infoHash = infoHash;
|
||||
m.infoHashHex = infoHash != null ? Crypto.toHex(infoHash) : null;
|
||||
var infoDict = Bencode.decode(rawInfo);
|
||||
m.parseInfo(infoDict);
|
||||
if (m.metaVersion == 2) {
|
||||
m.infoHashV2 = haxe.crypto.Sha256.make(rawInfo);
|
||||
m.infoHashV2Hex = Crypto.toHex(m.infoHashV2);
|
||||
}
|
||||
m.announce = announce != null ? announce : [];
|
||||
m.initFileHashes();
|
||||
return m;
|
||||
}
|
||||
|
||||
public static function create(name:String, files:Array<{path:String, data:Bytes}>, ?announce:Array<String>, ?opts:Dynamic):MetaInfo {
|
||||
var m = new MetaInfo();
|
||||
m.name = name;
|
||||
m.announce = announce != null ? announce : [];
|
||||
m.files = [];
|
||||
var pieceLengthOpt = 0;
|
||||
if (opts != null) {
|
||||
m.isPrivate = fieldFloat(opts, "private") == 1
|
||||
|| Reflect.field(opts, "private") == true;
|
||||
m.source = Reflect.field(opts, "source");
|
||||
m.comment = Reflect.field(opts, "comment");
|
||||
m.createdBy = Reflect.field(opts, "createdBy");
|
||||
var u:Dynamic = Reflect.field(opts, "urlList");
|
||||
if (u != null) {
|
||||
m.urlList = Std.isOfType(u, Array)
|
||||
? (u:Array<String>) : [Std.string(u)];
|
||||
}
|
||||
pieceLengthOpt = Std.int(fieldFloat(opts, "pieceLength"));
|
||||
}
|
||||
|
||||
var mvOpt = opts != null
|
||||
? Std.int(fieldFloat(opts, "metaVersion")) : 1;
|
||||
if (mvOpt < 1) mvOpt = 1;
|
||||
var isV1 = mvOpt != 2;
|
||||
var isV2 = mvOpt != 1;
|
||||
m.metaVersion = isV2 ? 2 : 1;
|
||||
|
||||
var aligned = mvOpt == 2;
|
||||
var total = 0;
|
||||
for (f in files) total += f.data.length;
|
||||
m.pieceLength = pieceLengthOpt > 0 ? pieceLengthOpt
|
||||
: TorrentInfo.pieceLength(total);
|
||||
|
||||
var cursor = 0;
|
||||
var content = new haxe.io.BytesBuffer();
|
||||
for (f in files) {
|
||||
var entry:TorrentFileEntry = {path: f.path,
|
||||
length: f.data.length,
|
||||
offset: aligned ? cursor * m.pieceLength
|
||||
: m.totalLength};
|
||||
if (isV2) entry.piecesRoot = merkleRoot(f.data);
|
||||
m.files.push(entry);
|
||||
m.totalLength += f.data.length;
|
||||
cursor += Std.int(Math.ceil(f.data.length
|
||||
/ m.pieceLength));
|
||||
content.addBytes(f.data, 0, f.data.length);
|
||||
}
|
||||
var all = content.getBytes();
|
||||
m.storageLength = aligned ? cursor * m.pieceLength
|
||||
: m.totalLength;
|
||||
m.pieces = isV1 ? TorrentInfo.pieces(all, m.pieceLength)
|
||||
: null;
|
||||
|
||||
var info:Dynamic = {};
|
||||
Reflect.setField(info, "name", name);
|
||||
Reflect.setField(info, "piece length", m.pieceLength);
|
||||
if (m.isPrivate) Reflect.setField(info, "private", 1);
|
||||
if (m.source != null) Reflect.setField(info, "source", m.source);
|
||||
if (isV1) {
|
||||
Reflect.setField(info, "pieces", m.pieces);
|
||||
if (files.length == 1) {
|
||||
Reflect.setField(info, "length", files[0].data.length);
|
||||
} else {
|
||||
var fl:Array<Dynamic> = [];
|
||||
for (f in files) {
|
||||
var fd:Dynamic = {};
|
||||
Reflect.setField(fd, "length", f.data.length);
|
||||
var rel = f.path;
|
||||
if (rel.indexOf(name + "/") == 0) {
|
||||
rel = rel.substr(name.length + 1);
|
||||
}
|
||||
Reflect.setField(fd, "path", rel.split("/"));
|
||||
fl.push(fd);
|
||||
}
|
||||
Reflect.setField(info, "files", fl);
|
||||
}
|
||||
}
|
||||
if (isV2) {
|
||||
Reflect.setField(info, "meta version", 2);
|
||||
Reflect.setField(info, "file tree",
|
||||
buildFileTree(name, files, m.files));
|
||||
}
|
||||
if (isV2) {
|
||||
m.pieceLayers = [];
|
||||
for (i in 0...files.length) {
|
||||
var f = files[i];
|
||||
var entry = m.files[i];
|
||||
if (f.data.length <= m.pieceLength
|
||||
|| entry.piecesRoot == null) continue;
|
||||
var np = Std.int(Math.ceil(f.data.length
|
||||
/ m.pieceLength));
|
||||
var buf = new haxe.io.BytesBuffer();
|
||||
for (p in 0...np) {
|
||||
var off = p * m.pieceLength;
|
||||
var sz = Std.int(Math.min(m.pieceLength,
|
||||
f.data.length - off));
|
||||
buf.addBytes(m.pieceRootV2(
|
||||
f.data.sub(off, sz)), 0, 32);
|
||||
}
|
||||
m.pieceLayers.push({root: entry.piecesRoot,
|
||||
hashes: buf.getBytes()});
|
||||
}
|
||||
if (m.pieceLayers.length == 0) m.pieceLayers = null;
|
||||
}
|
||||
m.rawInfo = Bencode.encode(info);
|
||||
if (isV1) {
|
||||
m.infoHash = haxe.crypto.Sha1.make(m.rawInfo);
|
||||
m.infoHashHex = Crypto.toHex(m.infoHash);
|
||||
}
|
||||
if (isV2) {
|
||||
m.infoHashV2 = haxe.crypto.Sha256.make(m.rawInfo);
|
||||
m.infoHashV2Hex = Crypto.toHex(m.infoHashV2);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static function merkleRoot(data:Bytes):Bytes {
|
||||
if (data.length == 0) return Bytes.alloc(32);
|
||||
var leaves:Array<Bytes> = [];
|
||||
var pos = 0;
|
||||
while (pos < data.length) {
|
||||
var n = Std.int(Math.min(16384, data.length - pos));
|
||||
leaves.push(haxe.crypto.Sha256.make(data.sub(pos, n)));
|
||||
pos += n;
|
||||
}
|
||||
var pow = 1;
|
||||
while (pow < leaves.length) pow <<= 1;
|
||||
var zero = leafZero();
|
||||
while (leaves.length < pow) leaves.push(zero);
|
||||
return merkleReduce(leaves);
|
||||
}
|
||||
|
||||
static var leafZeroCache:Bytes = null;
|
||||
static function leafZero():Bytes {
|
||||
if (leafZeroCache == null) leafZeroCache = Bytes.alloc(32);
|
||||
return leafZeroCache;
|
||||
}
|
||||
|
||||
static function buildFileTree(name:String, files:Array<{path:String, data:Bytes}>, entries:Array<TorrentFileEntry>):Dynamic {
|
||||
var root:Dynamic = {};
|
||||
for (i in 0...files.length) {
|
||||
var rel = files[i].path;
|
||||
if (rel.indexOf(name + "/") == 0) {
|
||||
rel = rel.substr(name.length + 1);
|
||||
}
|
||||
var parts = rel.split("/");
|
||||
var node = root;
|
||||
for (p in 0...parts.length - 1) {
|
||||
var nxt = Reflect.field(node, parts[p]);
|
||||
if (nxt == null) {
|
||||
nxt = {};
|
||||
Reflect.setField(node, parts[p], nxt);
|
||||
}
|
||||
node = nxt;
|
||||
}
|
||||
var leaf:Dynamic = {};
|
||||
Reflect.setField(leaf, "length", files[i].data.length);
|
||||
Reflect.setField(leaf, "pieces root",
|
||||
entries[i].piecesRoot);
|
||||
var leafWrap:Dynamic = {};
|
||||
Reflect.setField(leafWrap, "", leaf);
|
||||
Reflect.setField(node, parts[parts.length - 1], leafWrap);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
public function toTorrentBytes(?extra:Dynamic):Bytes {
|
||||
var dict:Dynamic = extra != null ? extra : {};
|
||||
Reflect.setField(dict, "info", Bencode.decode(rawInfo));
|
||||
if (announce.length > 0) {
|
||||
Reflect.setField(dict, "announce", announce[0]);
|
||||
var tiers:Array<Dynamic> = [for (a in announce) [a]];
|
||||
Reflect.setField(dict, "announce-list", tiers);
|
||||
}
|
||||
if (comment != null) Reflect.setField(dict, "comment", comment);
|
||||
if (createdBy != null) {
|
||||
Reflect.setField(dict, "created by", createdBy);
|
||||
}
|
||||
if (urlList != null && urlList.length > 0) {
|
||||
Reflect.setField(dict, "url-list",
|
||||
urlList.length == 1 ? (urlList[0]:Dynamic)
|
||||
: [for (u in urlList) u]);
|
||||
}
|
||||
Reflect.setField(dict, "creation date",
|
||||
creationDate > 0 ? creationDate
|
||||
: Std.int(Date.now().getTime() / 1000));
|
||||
if (pieceLayers != null && pieceLayers.length > 0) {
|
||||
var pl = new Bencode.RawDict();
|
||||
var sorted = pieceLayers.copy();
|
||||
sorted.sort(function(a, b) {
|
||||
var n = a.root.length < b.root.length
|
||||
? a.root.length : b.root.length;
|
||||
for (i in 0...n) {
|
||||
var d = a.root.get(i) - b.root.get(i);
|
||||
if (d != 0) return d;
|
||||
}
|
||||
return a.root.length - b.root.length;
|
||||
});
|
||||
for (e in sorted) {
|
||||
pl.pairs.push({key: e.root, val: e.hashes});
|
||||
}
|
||||
Reflect.setField(dict, "piece layers", pl);
|
||||
}
|
||||
return Bencode.encode(dict);
|
||||
}
|
||||
|
||||
public function pieceHash(index:Int):Bytes {
|
||||
return pieces.sub(index * TorrentInfo.HASH_LENGTH,
|
||||
TorrentInfo.HASH_LENGTH);
|
||||
}
|
||||
|
||||
public function pieceSize(index:Int):Int {
|
||||
if (pieces == null) return pieceSizeV2(index); // v2 aligned
|
||||
if (index == numPieces - 1) {
|
||||
var rem = totalLength % pieceLength;
|
||||
return rem == 0 ? pieceLength : rem;
|
||||
}
|
||||
return pieceLength;
|
||||
}
|
||||
|
||||
static function fieldFloat(obj:Dynamic, name:String):Float {
|
||||
var v = Reflect.field(obj, name);
|
||||
if (v == null) return 0;
|
||||
if (Std.isOfType(v, Float) || Std.isOfType(v, Int)) return v;
|
||||
return Std.parseFloat(Std.string(v));
|
||||
}
|
||||
|
||||
static function bytesToString(v:Dynamic):String {
|
||||
if (v == null) return null;
|
||||
if (Std.isOfType(v, String)) return v;
|
||||
if (Std.isOfType(v, Float) || Std.isOfType(v, Int)) {
|
||||
return Std.string(v);
|
||||
}
|
||||
if (Std.isOfType(v, Bytes)) return (v:Bytes).toString();
|
||||
return Std.string(v);
|
||||
}
|
||||
|
||||
public static function toBytes(v:Dynamic):Bytes {
|
||||
if (v == null) return null;
|
||||
if (Std.isOfType(v, Float) || Std.isOfType(v, Int)
|
||||
|| Std.isOfType(v, String) || Std.isOfType(v, Array)) {
|
||||
return null;
|
||||
}
|
||||
if (Std.isOfType(v, Bytes)) return (v:Bytes);
|
||||
return null;
|
||||
}
|
||||
|
||||
static function extractInfoBytes(data:Bytes):Bytes {
|
||||
var pos = 0;
|
||||
if (data.get(pos) != 'd'.code) throw "MetaInfo: not a dict";
|
||||
pos++;
|
||||
while (pos < data.length && data.get(pos) != 'e'.code) {
|
||||
var keyEnd = skipValue(data, pos);
|
||||
var key = data.sub(pos, keyEnd - pos).toString();
|
||||
var colon = key.indexOf(":");
|
||||
key = key.substr(colon + 1);
|
||||
var valStart = keyEnd;
|
||||
var valEnd = skipValue(data, valStart);
|
||||
if (key == "info") {
|
||||
return data.sub(valStart, valEnd - valStart);
|
||||
}
|
||||
pos = valEnd;
|
||||
}
|
||||
throw "MetaInfo: info key not found";
|
||||
}
|
||||
|
||||
static public function skipValue(data:Bytes, pos:Int):Int {
|
||||
var b = data.get(pos);
|
||||
if (b == 'i'.code) {
|
||||
pos++;
|
||||
while (pos < data.length && data.get(pos) != 'e'.code) pos++;
|
||||
return pos + 1;
|
||||
}
|
||||
if (b == 'l'.code || b == 'd'.code) {
|
||||
pos++;
|
||||
while (pos < data.length && data.get(pos) != 'e'.code) {
|
||||
pos = skipValue(data, pos);
|
||||
}
|
||||
return pos + 1;
|
||||
}
|
||||
var len = 0;
|
||||
while (pos < data.length && data.get(pos) != ':'.code) {
|
||||
len = len * 10 + (data.get(pos) - '0'.code);
|
||||
pos++;
|
||||
}
|
||||
return pos + 1 + len;
|
||||
}
|
||||
}
|
||||
329
leenkx/Sources/leenkx/network/torrent/PieceManager.hx
Normal file
329
leenkx/Sources/leenkx/network/torrent/PieceManager.hx
Normal file
@ -0,0 +1,329 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.storage.Storage.IStorage;
|
||||
|
||||
class PieceManager {
|
||||
public static inline var BLOCK_SIZE = 16384;
|
||||
|
||||
var meta:MetaInfo;
|
||||
var storage:IStorage;
|
||||
|
||||
public var have:Array<Bool>;
|
||||
public var numHave:Int = 0;
|
||||
public var bitfield:Bytes;
|
||||
|
||||
var pending:Map<Int, Map<Int, Bytes>> = [];
|
||||
var pendingCount:Map<Int, Int> = [];
|
||||
var requested:Map<String, Map<Int, Map<Int, Bool>>> = [];
|
||||
var globalRequested:Map<Int, Map<Int, Bool>> = [];
|
||||
var pendingV2:Map<Int, Bytes> = [];
|
||||
|
||||
public var onPieceComplete:Int -> Void;
|
||||
public var onPieceFailed:Int -> Void;
|
||||
public var onComplete:Void -> Void;
|
||||
|
||||
@:allow(leenkx.network.torrent.Torrent)
|
||||
@:allow(leenkx.network.torrent.WebSeed)
|
||||
var selections:Array<{start:Int, end:Int, priority:Int,
|
||||
notify:Void -> Void}> = [];
|
||||
|
||||
public function select(startByte:Int, endByte:Int, ?priority:Int, ?notify:Void -> Void):Void {
|
||||
selections.push({start: pieceIndex(startByte),
|
||||
end: pieceIndex(endByte),
|
||||
priority: priority != null ? priority : 0,
|
||||
notify: notify});
|
||||
selections.sort(function(a, b) return b.priority
|
||||
- a.priority);
|
||||
}
|
||||
|
||||
public function deselect(startByte:Int, endByte:Int, ?priority:Int):Void {
|
||||
var s = pieceIndex(startByte);
|
||||
var e = pieceIndex(endByte);
|
||||
var p = priority != null ? priority : 0;
|
||||
for (i in 0...selections.length) {
|
||||
var sel = selections[i];
|
||||
if (sel.start == s && sel.end == e
|
||||
&& sel.priority == p) {
|
||||
selections.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function pieceIndex(byte:Int):Int {
|
||||
var i = Std.int(byte / meta.pieceLength);
|
||||
if (i < 0) i = 0;
|
||||
if (i >= meta.numPieces) i = meta.numPieces - 1;
|
||||
return i;
|
||||
}
|
||||
|
||||
function updateSelections(index:Int):Void {
|
||||
var i = 0;
|
||||
while (i < selections.length) {
|
||||
var sel = selections[i];
|
||||
if (index < sel.start || index > sel.end
|
||||
|| sel.notify == null) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
var done = true;
|
||||
for (p in sel.start...sel.end + 1) {
|
||||
if (!have[p]) {
|
||||
done = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (done) {
|
||||
selections.splice(i, 1);
|
||||
sel.notify();
|
||||
} else i++;
|
||||
}
|
||||
}
|
||||
|
||||
public var complete(get, never):Bool;
|
||||
function get_complete():Bool return numHave == meta.numPieces;
|
||||
|
||||
public function new(meta:MetaInfo, storage:IStorage) {
|
||||
this.meta = meta;
|
||||
this.storage = storage;
|
||||
have = [for (i in 0...meta.numPieces) false];
|
||||
bitfield = Bytes.alloc(Std.int(Math.ceil(meta.numPieces / 8)));
|
||||
}
|
||||
|
||||
public static function forSeeder(meta:MetaInfo, storage:IStorage):PieceManager {
|
||||
var pm = new PieceManager(meta, storage);
|
||||
for (i in 0...meta.numPieces) pm.markHave(i);
|
||||
return pm;
|
||||
}
|
||||
|
||||
public function markHave(index:Int):Void {
|
||||
if (have[index]) return;
|
||||
have[index] = true;
|
||||
numHave++;
|
||||
bitfield.set(index >> 3, bitfield.get(index >> 3)
|
||||
| (0x80 >> (index & 7)));
|
||||
}
|
||||
|
||||
public function addBlock(index:Int, begin:Int, data:Bytes):Bool {
|
||||
if (index < 0 || index >= meta.numPieces || begin < 0
|
||||
|| data == null
|
||||
|| begin + data.length > meta.pieceSize(index)) {
|
||||
return false;
|
||||
}
|
||||
if (have[index]) return true;
|
||||
var blocks = pending.get(index);
|
||||
if (blocks == null) {
|
||||
blocks = [];
|
||||
pending.set(index, blocks);
|
||||
pendingCount.set(index, 0);
|
||||
}
|
||||
if (!blocks.exists(begin)) {
|
||||
blocks.set(begin, data);
|
||||
pendingCount.set(index, pendingCount.get(index) + data.length);
|
||||
var gReq = globalRequested.get(index);
|
||||
if (gReq != null) {
|
||||
gReq.remove(begin);
|
||||
if (!gReq.keys().hasNext()) globalRequested.remove(index);
|
||||
}
|
||||
}
|
||||
if (pendingCount.get(index) < meta.pieceSize(index)) return false;
|
||||
|
||||
var piece = Bytes.alloc(meta.pieceSize(index));
|
||||
var off = 0;
|
||||
var keys = [for (k in blocks.keys()) k];
|
||||
keys.sort(Reflect.compare);
|
||||
for (b in keys) {
|
||||
var d = blocks.get(b);
|
||||
piece.blit(b, d, 0, d.length);
|
||||
}
|
||||
var ok = true;
|
||||
if (meta.pieces == null) {
|
||||
var fi = meta.fileForPiece(index);
|
||||
var layer = fi >= 0 && meta.fileHashes != null
|
||||
&& meta.fileHashes.length > fi
|
||||
? meta.fileHashes[fi] : null;
|
||||
pending.remove(index);
|
||||
pendingCount.remove(index);
|
||||
if (layer == null) {
|
||||
pendingV2.set(index, piece);
|
||||
return false;
|
||||
}
|
||||
ok = meta.verifyPieceV2(index, piece);
|
||||
} else {
|
||||
var hash = haxe.crypto.Sha1.make(piece);
|
||||
ok = MetaInfo.memcmp(hash, meta.pieceHash(index));
|
||||
pending.remove(index);
|
||||
pendingCount.remove(index);
|
||||
}
|
||||
if (!ok) {
|
||||
trace('[PieceManager] piece ' + index
|
||||
+ ' verification FAILED');
|
||||
releaseRequested(index);
|
||||
if (onPieceFailed != null) onPieceFailed(index);
|
||||
return false;
|
||||
}
|
||||
storage.write(index * meta.pieceLength, piece);
|
||||
markHave(index);
|
||||
updateSelections(index);
|
||||
if (onPieceComplete != null) onPieceComplete(index);
|
||||
if (complete && onComplete != null) onComplete();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function flushPendingV2(fi:Int):Bool {
|
||||
if (meta.pieces != null) return false;
|
||||
var f = meta.files[fi];
|
||||
var start = Std.int(f.offset / meta.pieceLength);
|
||||
var np = Std.int(Math.ceil(f.length / meta.pieceLength));
|
||||
var wrote = false;
|
||||
for (i in start...start + np) {
|
||||
var piece = pendingV2.get(i);
|
||||
if (piece == null) continue;
|
||||
pendingV2.remove(i);
|
||||
if (!meta.verifyPieceV2(i, piece)) {
|
||||
trace('[PieceManager] flush piece ' + i
|
||||
+ ' verification FAILED');
|
||||
releaseRequested(i);
|
||||
if (onPieceFailed != null) onPieceFailed(i);
|
||||
continue;
|
||||
}
|
||||
storage.write(i * meta.pieceLength, piece);
|
||||
markHave(i);
|
||||
updateSelections(i);
|
||||
wrote = true;
|
||||
if (onPieceComplete != null) onPieceComplete(i);
|
||||
}
|
||||
if (complete && onComplete != null) onComplete();
|
||||
return wrote;
|
||||
}
|
||||
|
||||
public function untouched(index:Int):Bool {
|
||||
return !have[index] && !pending.exists(index)
|
||||
&& !pendingV2.exists(index)
|
||||
&& !globalRequested.exists(index);
|
||||
}
|
||||
|
||||
public function readBlock(index:Int, begin:Int, length:Int):Bytes {
|
||||
return storage.read(index * meta.pieceLength + begin, length);
|
||||
}
|
||||
|
||||
public function neededBlocks(peerKey:String, index:Int):Array<{begin:Int, length:Int}> {
|
||||
if (pendingV2.exists(index)) return [];
|
||||
var size = meta.pieceSize(index);
|
||||
var blocks = pending.get(index);
|
||||
var peerReq = requested.get(peerKey);
|
||||
var req = peerReq != null ? peerReq.get(index) : null;
|
||||
var globalReq = globalRequested.get(index);
|
||||
var out:Array<{begin:Int, length:Int}> = [];
|
||||
var off = 0;
|
||||
while (off < size) {
|
||||
var len = size - off;
|
||||
if (len > BLOCK_SIZE) len = BLOCK_SIZE;
|
||||
if ((blocks == null || !blocks.exists(off))
|
||||
&& (req == null || !req.exists(off))
|
||||
&& (globalReq == null || !globalReq.exists(off))) {
|
||||
out.push({begin: off, length: len});
|
||||
}
|
||||
off += len;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public function markRequested(key:String, index:Int, begin:Int):Void {
|
||||
var r = requested.get(key);
|
||||
if (r == null) requested.set(key,
|
||||
r = new Map<Int, Map<Int, Bool>>());
|
||||
var m = r.get(index);
|
||||
if (m == null) r.set(index, m = new Map<Int, Bool>());
|
||||
m.set(begin, true);
|
||||
var g = globalRequested.get(index);
|
||||
if (g == null) globalRequested.set(index,
|
||||
g = new Map<Int, Bool>());
|
||||
g.set(begin, true);
|
||||
reqTimes.set(key + "|" + index + "|" + begin,
|
||||
haxe.Timer.stamp());
|
||||
}
|
||||
|
||||
var reqTimes:Map<String, Float> = [];
|
||||
|
||||
public function expireRequests(ttlSec:Float):Map<String, Int> {
|
||||
var now = haxe.Timer.stamp();
|
||||
var dead:Array<String> = [];
|
||||
for (k in reqTimes.keys()) {
|
||||
if (now - reqTimes.get(k) > ttlSec) dead.push(k);
|
||||
}
|
||||
var expired:Map<String, Int> = [];
|
||||
for (k in dead) {
|
||||
var p = k.split("|");
|
||||
var idx = Std.parseInt(p[1]);
|
||||
var beg = Std.parseInt(p[2]);
|
||||
var peerReq = requested.get(p[0]);
|
||||
if (peerReq != null) {
|
||||
var r = peerReq.get(idx);
|
||||
if (r != null && r.exists(beg)) {
|
||||
r.remove(beg);
|
||||
var n = expired.exists(p[0]) ? expired.get(p[0]) : 0;
|
||||
expired.set(p[0], n + 1);
|
||||
}
|
||||
}
|
||||
var g = globalRequested.get(idx);
|
||||
if (g != null) {
|
||||
g.remove(beg);
|
||||
var empty = true;
|
||||
for (b in g.keys()) {
|
||||
empty = false;
|
||||
break;
|
||||
}
|
||||
if (empty) globalRequested.remove(idx);
|
||||
}
|
||||
reqTimes.remove(k);
|
||||
}
|
||||
return expired;
|
||||
}
|
||||
|
||||
public function releaseRequested(index:Int):Void {
|
||||
globalRequested.remove(index);
|
||||
var keys = [for (k in requested.keys()) k];
|
||||
for (key in keys) {
|
||||
var r = requested.get(key);
|
||||
var m = r.get(index);
|
||||
if (m != null) {
|
||||
for (b in m.keys()) {
|
||||
reqTimes.remove(key + "|" + index + "|" + b);
|
||||
}
|
||||
r.remove(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function clearRequested(peerKey:String, index:Int):Void {
|
||||
var peerReq = requested.get(peerKey);
|
||||
if (peerReq != null) {
|
||||
var req = peerReq.get(index);
|
||||
if (req != null) {
|
||||
var gReq = globalRequested.get(index);
|
||||
if (gReq != null) {
|
||||
for (off in req.keys()) gReq.remove(off);
|
||||
if (!gReq.keys().hasNext()) globalRequested.remove(index);
|
||||
}
|
||||
}
|
||||
peerReq.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
public function clearAllRequested(peerKey:String):Void {
|
||||
var peerReq = requested.get(peerKey);
|
||||
if (peerReq != null) {
|
||||
for (index in peerReq.keys()) {
|
||||
var req = peerReq.get(index);
|
||||
var gReq = globalRequested.get(index);
|
||||
if (gReq != null && req != null) {
|
||||
for (off in req.keys()) gReq.remove(off);
|
||||
if (!gReq.keys().hasNext()) globalRequested.remove(index);
|
||||
}
|
||||
}
|
||||
requested.remove(peerKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
334
leenkx/Sources/leenkx/network/torrent/Ripemd160.hx
Normal file
334
leenkx/Sources/leenkx/network/torrent/Ripemd160.hx
Normal file
@ -0,0 +1,334 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class Ripemd160 {
|
||||
|
||||
static inline function ROL(x:Int, n:Int):Int {
|
||||
return (x << n) | (x >>> (32 - n));
|
||||
}
|
||||
|
||||
static inline function F(x:Int, y:Int, z:Int):Int {
|
||||
return x ^ y ^ z;
|
||||
}
|
||||
|
||||
static inline function G(x:Int, y:Int, z:Int):Int {
|
||||
return (x & y) | (~x & z);
|
||||
}
|
||||
|
||||
static inline function H(x:Int, y:Int, z:Int):Int {
|
||||
return (x | ~y) ^ z;
|
||||
}
|
||||
|
||||
static inline function I(x:Int, y:Int, z:Int):Int {
|
||||
return (x & z) | (y & ~z);
|
||||
}
|
||||
|
||||
static inline function J(x:Int, y:Int, z:Int):Int {
|
||||
return x ^ (y | ~z);
|
||||
}
|
||||
|
||||
static inline function FF(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + F(b, c, d) + x;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function GG(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + G(b, c, d) + x + 0x5A827999;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function HH(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + H(b, c, d) + x + 0x6ED9EBA1;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function II(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + I(b, c, d) + x + 0x8F1BBCDC;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function JJ(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + J(b, c, d) + x + 0xA953FD4E;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function FFF(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + F(b, c, d) + x;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function GGG(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + G(b, c, d) + x + 0x7A6D76E9;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function HHH(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + H(b, c, d) + x + 0x6D703EF3;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function III(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + I(b, c, d) + x + 0x5C4DD124;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
static inline function JJJ(a:Int, b:Int, c:Int, d:Int, e:Int, x:Int, s:Int):Int {
|
||||
a = a + J(b, c, d) + x + 0x50A28BE6;
|
||||
a = ROL(a, s) + e;
|
||||
return a;
|
||||
}
|
||||
|
||||
public static function make(data:Bytes):Bytes {
|
||||
var msgLen = data.length;
|
||||
var bitLen = msgLen * 8;
|
||||
|
||||
var padLen = 56 - ((msgLen + 1) % 64);
|
||||
if (padLen < 0) padLen += 64;
|
||||
var totalLen = msgLen + 1 + padLen + 8;
|
||||
|
||||
var padded = Bytes.alloc(totalLen);
|
||||
padded.blit(0, data, 0, msgLen);
|
||||
padded.set(msgLen, 0x80);
|
||||
for (i in (msgLen + 1)...(totalLen - 8)) padded.set(i, 0);
|
||||
padded.set(totalLen - 8, bitLen & 0xFF);
|
||||
padded.set(totalLen - 7, (bitLen >> 8) & 0xFF);
|
||||
padded.set(totalLen - 6, (bitLen >> 16) & 0xFF);
|
||||
padded.set(totalLen - 5, (bitLen >> 24) & 0xFF);
|
||||
padded.set(totalLen - 4, 0);
|
||||
padded.set(totalLen - 3, 0);
|
||||
padded.set(totalLen - 2, 0);
|
||||
padded.set(totalLen - 1, 0);
|
||||
|
||||
var h0 = 0x67452301;
|
||||
var h1 = 0xEFCDAB89;
|
||||
var h2 = 0x98BADCFE;
|
||||
var h3 = 0x10325476;
|
||||
var h4 = 0xC3D2E1F0;
|
||||
|
||||
var numBlocks = Std.int(totalLen / 64);
|
||||
|
||||
for (block in 0...numBlocks) {
|
||||
var base = block * 64;
|
||||
var X = new Array<Int>();
|
||||
for (i in 0...16) {
|
||||
var off = base + i * 4;
|
||||
X.push(padded.get(off) | (padded.get(off + 1) << 8) |
|
||||
(padded.get(off + 2) << 16) | (padded.get(off + 3) << 24));
|
||||
}
|
||||
|
||||
var a = h0, b = h1, c = h2, d = h3, e = h4;
|
||||
var a1 = h0, b1 = h1, c1 = h2, d1 = h3, e1 = h4;
|
||||
|
||||
a = FF(a, b, c, d, e, X[0], 11); c = ROL(c, 10);
|
||||
e = FF(e, a, b, c, d, X[1], 14); b = ROL(b, 10);
|
||||
d = FF(d, e, a, b, c, X[2], 15); a = ROL(a, 10);
|
||||
c = FF(c, d, e, a, b, X[3], 12); e = ROL(e, 10);
|
||||
b = FF(b, c, d, e, a, X[4], 5); d = ROL(d, 10);
|
||||
a = FF(a, b, c, d, e, X[5], 8); c = ROL(c, 10);
|
||||
e = FF(e, a, b, c, d, X[6], 7); b = ROL(b, 10);
|
||||
d = FF(d, e, a, b, c, X[7], 9); a = ROL(a, 10);
|
||||
c = FF(c, d, e, a, b, X[8], 11); e = ROL(e, 10);
|
||||
b = FF(b, c, d, e, a, X[9], 13); d = ROL(d, 10);
|
||||
a = FF(a, b, c, d, e, X[10], 14); c = ROL(c, 10);
|
||||
e = FF(e, a, b, c, d, X[11], 15); b = ROL(b, 10);
|
||||
d = FF(d, e, a, b, c, X[12], 6); a = ROL(a, 10);
|
||||
c = FF(c, d, e, a, b, X[13], 7); e = ROL(e, 10);
|
||||
b = FF(b, c, d, e, a, X[14], 9); d = ROL(d, 10);
|
||||
a = FF(a, b, c, d, e, X[15], 8); c = ROL(c, 10);
|
||||
|
||||
e = GG(e, a, b, c, d, X[7], 7); b = ROL(b, 10);
|
||||
d = GG(d, e, a, b, c, X[4], 6); a = ROL(a, 10);
|
||||
c = GG(c, d, e, a, b, X[13], 8); e = ROL(e, 10);
|
||||
b = GG(b, c, d, e, a, X[1], 13); d = ROL(d, 10);
|
||||
a = GG(a, b, c, d, e, X[10], 11); c = ROL(c, 10);
|
||||
e = GG(e, a, b, c, d, X[6], 9); b = ROL(b, 10);
|
||||
d = GG(d, e, a, b, c, X[15], 7); a = ROL(a, 10);
|
||||
c = GG(c, d, e, a, b, X[3], 15); e = ROL(e, 10);
|
||||
b = GG(b, c, d, e, a, X[12], 7); d = ROL(d, 10);
|
||||
a = GG(a, b, c, d, e, X[0], 12); c = ROL(c, 10);
|
||||
e = GG(e, a, b, c, d, X[9], 15); b = ROL(b, 10);
|
||||
d = GG(d, e, a, b, c, X[5], 9); a = ROL(a, 10);
|
||||
c = GG(c, d, e, a, b, X[2], 11); e = ROL(e, 10);
|
||||
b = GG(b, c, d, e, a, X[14], 7); d = ROL(d, 10);
|
||||
a = GG(a, b, c, d, e, X[11], 13); c = ROL(c, 10);
|
||||
e = GG(e, a, b, c, d, X[8], 12); b = ROL(b, 10);
|
||||
|
||||
d = HH(d, e, a, b, c, X[3], 11); a = ROL(a, 10);
|
||||
c = HH(c, d, e, a, b, X[10], 13); e = ROL(e, 10);
|
||||
b = HH(b, c, d, e, a, X[14], 6); d = ROL(d, 10);
|
||||
a = HH(a, b, c, d, e, X[4], 7); c = ROL(c, 10);
|
||||
e = HH(e, a, b, c, d, X[9], 14); b = ROL(b, 10);
|
||||
d = HH(d, e, a, b, c, X[15], 9); a = ROL(a, 10);
|
||||
c = HH(c, d, e, a, b, X[8], 13); e = ROL(e, 10);
|
||||
b = HH(b, c, d, e, a, X[1], 15); d = ROL(d, 10);
|
||||
a = HH(a, b, c, d, e, X[2], 14); c = ROL(c, 10);
|
||||
e = HH(e, a, b, c, d, X[7], 8); b = ROL(b, 10);
|
||||
d = HH(d, e, a, b, c, X[0], 13); a = ROL(a, 10);
|
||||
c = HH(c, d, e, a, b, X[6], 6); e = ROL(e, 10);
|
||||
b = HH(b, c, d, e, a, X[13], 5); d = ROL(d, 10);
|
||||
a = HH(a, b, c, d, e, X[11], 12); c = ROL(c, 10);
|
||||
e = HH(e, a, b, c, d, X[5], 7); b = ROL(b, 10);
|
||||
d = HH(d, e, a, b, c, X[12], 5); a = ROL(a, 10);
|
||||
|
||||
c = II(c, d, e, a, b, X[1], 11); e = ROL(e, 10);
|
||||
b = II(b, c, d, e, a, X[9], 12); d = ROL(d, 10);
|
||||
a = II(a, b, c, d, e, X[11], 14); c = ROL(c, 10);
|
||||
e = II(e, a, b, c, d, X[10], 15); b = ROL(b, 10);
|
||||
d = II(d, e, a, b, c, X[0], 14); a = ROL(a, 10);
|
||||
c = II(c, d, e, a, b, X[8], 15); e = ROL(e, 10);
|
||||
b = II(b, c, d, e, a, X[12], 9); d = ROL(d, 10);
|
||||
a = II(a, b, c, d, e, X[4], 8); c = ROL(c, 10);
|
||||
e = II(e, a, b, c, d, X[13], 9); b = ROL(b, 10);
|
||||
d = II(d, e, a, b, c, X[3], 14); a = ROL(a, 10);
|
||||
c = II(c, d, e, a, b, X[7], 5); e = ROL(e, 10);
|
||||
b = II(b, c, d, e, a, X[15], 6); d = ROL(d, 10);
|
||||
a = II(a, b, c, d, e, X[14], 8); c = ROL(c, 10);
|
||||
e = II(e, a, b, c, d, X[5], 6); b = ROL(b, 10);
|
||||
d = II(d, e, a, b, c, X[6], 5); a = ROL(a, 10);
|
||||
c = II(c, d, e, a, b, X[2], 12); e = ROL(e, 10);
|
||||
|
||||
b = JJ(b, c, d, e, a, X[4], 9); d = ROL(d, 10);
|
||||
a = JJ(a, b, c, d, e, X[0], 15); c = ROL(c, 10);
|
||||
e = JJ(e, a, b, c, d, X[5], 5); b = ROL(b, 10);
|
||||
d = JJ(d, e, a, b, c, X[9], 11); a = ROL(a, 10);
|
||||
c = JJ(c, d, e, a, b, X[7], 6); e = ROL(e, 10);
|
||||
b = JJ(b, c, d, e, a, X[12], 8); d = ROL(d, 10);
|
||||
a = JJ(a, b, c, d, e, X[2], 13); c = ROL(c, 10);
|
||||
e = JJ(e, a, b, c, d, X[10], 12); b = ROL(b, 10);
|
||||
d = JJ(d, e, a, b, c, X[14], 5); a = ROL(a, 10);
|
||||
c = JJ(c, d, e, a, b, X[1], 12); e = ROL(e, 10);
|
||||
b = JJ(b, c, d, e, a, X[3], 13); d = ROL(d, 10);
|
||||
a = JJ(a, b, c, d, e, X[8], 14); c = ROL(c, 10);
|
||||
e = JJ(e, a, b, c, d, X[11], 11); b = ROL(b, 10);
|
||||
d = JJ(d, e, a, b, c, X[6], 8); a = ROL(a, 10);
|
||||
c = JJ(c, d, e, a, b, X[15], 5); e = ROL(e, 10);
|
||||
b = JJ(b, c, d, e, a, X[13], 6); d = ROL(d, 10);
|
||||
|
||||
a1 = JJJ(a1, b1, c1, d1, e1, X[5], 8); c1 = ROL(c1, 10);
|
||||
e1 = JJJ(e1, a1, b1, c1, d1, X[14], 9); b1 = ROL(b1, 10);
|
||||
d1 = JJJ(d1, e1, a1, b1, c1, X[7], 9); a1 = ROL(a1, 10);
|
||||
c1 = JJJ(c1, d1, e1, a1, b1, X[0], 11); e1 = ROL(e1, 10);
|
||||
b1 = JJJ(b1, c1, d1, e1, a1, X[9], 13); d1 = ROL(d1, 10);
|
||||
a1 = JJJ(a1, b1, c1, d1, e1, X[2], 15); c1 = ROL(c1, 10);
|
||||
e1 = JJJ(e1, a1, b1, c1, d1, X[11], 15); b1 = ROL(b1, 10);
|
||||
d1 = JJJ(d1, e1, a1, b1, c1, X[4], 5); a1 = ROL(a1, 10);
|
||||
c1 = JJJ(c1, d1, e1, a1, b1, X[13], 7); e1 = ROL(e1, 10);
|
||||
b1 = JJJ(b1, c1, d1, e1, a1, X[6], 7); d1 = ROL(d1, 10);
|
||||
a1 = JJJ(a1, b1, c1, d1, e1, X[15], 8); c1 = ROL(c1, 10);
|
||||
e1 = JJJ(e1, a1, b1, c1, d1, X[8], 11); b1 = ROL(b1, 10);
|
||||
d1 = JJJ(d1, e1, a1, b1, c1, X[1], 14); a1 = ROL(a1, 10);
|
||||
c1 = JJJ(c1, d1, e1, a1, b1, X[10], 14); e1 = ROL(e1, 10);
|
||||
b1 = JJJ(b1, c1, d1, e1, a1, X[3], 12); d1 = ROL(d1, 10);
|
||||
a1 = JJJ(a1, b1, c1, d1, e1, X[12], 6); c1 = ROL(c1, 10);
|
||||
|
||||
e1 = III(e1, a1, b1, c1, d1, X[6], 9); b1 = ROL(b1, 10);
|
||||
d1 = III(d1, e1, a1, b1, c1, X[11], 13); a1 = ROL(a1, 10);
|
||||
c1 = III(c1, d1, e1, a1, b1, X[3], 15); e1 = ROL(e1, 10);
|
||||
b1 = III(b1, c1, d1, e1, a1, X[7], 7); d1 = ROL(d1, 10);
|
||||
a1 = III(a1, b1, c1, d1, e1, X[0], 12); c1 = ROL(c1, 10);
|
||||
e1 = III(e1, a1, b1, c1, d1, X[13], 8); b1 = ROL(b1, 10);
|
||||
d1 = III(d1, e1, a1, b1, c1, X[5], 9); a1 = ROL(a1, 10);
|
||||
c1 = III(c1, d1, e1, a1, b1, X[10], 11); e1 = ROL(e1, 10);
|
||||
b1 = III(b1, c1, d1, e1, a1, X[14], 7); d1 = ROL(d1, 10);
|
||||
a1 = III(a1, b1, c1, d1, e1, X[15], 7); c1 = ROL(c1, 10);
|
||||
e1 = III(e1, a1, b1, c1, d1, X[8], 12); b1 = ROL(b1, 10);
|
||||
d1 = III(d1, e1, a1, b1, c1, X[12], 7); a1 = ROL(a1, 10);
|
||||
c1 = III(c1, d1, e1, a1, b1, X[4], 6); e1 = ROL(e1, 10);
|
||||
b1 = III(b1, c1, d1, e1, a1, X[9], 15); d1 = ROL(d1, 10);
|
||||
a1 = III(a1, b1, c1, d1, e1, X[1], 13); c1 = ROL(c1, 10);
|
||||
e1 = III(e1, a1, b1, c1, d1, X[2], 11); b1 = ROL(b1, 10);
|
||||
|
||||
d1 = HHH(d1, e1, a1, b1, c1, X[15], 9); a1 = ROL(a1, 10);
|
||||
c1 = HHH(c1, d1, e1, a1, b1, X[5], 7); e1 = ROL(e1, 10);
|
||||
b1 = HHH(b1, c1, d1, e1, a1, X[1], 15); d1 = ROL(d1, 10);
|
||||
a1 = HHH(a1, b1, c1, d1, e1, X[3], 11); c1 = ROL(c1, 10);
|
||||
e1 = HHH(e1, a1, b1, c1, d1, X[7], 8); b1 = ROL(b1, 10);
|
||||
d1 = HHH(d1, e1, a1, b1, c1, X[14], 6); a1 = ROL(a1, 10);
|
||||
c1 = HHH(c1, d1, e1, a1, b1, X[6], 6); e1 = ROL(e1, 10);
|
||||
b1 = HHH(b1, c1, d1, e1, a1, X[9], 14); d1 = ROL(d1, 10);
|
||||
a1 = HHH(a1, b1, c1, d1, e1, X[11], 12); c1 = ROL(c1, 10);
|
||||
e1 = HHH(e1, a1, b1, c1, d1, X[8], 13); b1 = ROL(b1, 10);
|
||||
d1 = HHH(d1, e1, a1, b1, c1, X[12], 5); a1 = ROL(a1, 10);
|
||||
c1 = HHH(c1, d1, e1, a1, b1, X[2], 14); e1 = ROL(e1, 10);
|
||||
b1 = HHH(b1, c1, d1, e1, a1, X[10], 13); d1 = ROL(d1, 10);
|
||||
a1 = HHH(a1, b1, c1, d1, e1, X[0], 13); c1 = ROL(c1, 10);
|
||||
e1 = HHH(e1, a1, b1, c1, d1, X[4], 7); b1 = ROL(b1, 10);
|
||||
d1 = HHH(d1, e1, a1, b1, c1, X[13], 5); a1 = ROL(a1, 10);
|
||||
|
||||
c1 = GGG(c1, d1, e1, a1, b1, X[8], 15); e1 = ROL(e1, 10);
|
||||
b1 = GGG(b1, c1, d1, e1, a1, X[6], 5); d1 = ROL(d1, 10);
|
||||
a1 = GGG(a1, b1, c1, d1, e1, X[4], 8); c1 = ROL(c1, 10);
|
||||
e1 = GGG(e1, a1, b1, c1, d1, X[1], 11); b1 = ROL(b1, 10);
|
||||
d1 = GGG(d1, e1, a1, b1, c1, X[3], 14); a1 = ROL(a1, 10);
|
||||
c1 = GGG(c1, d1, e1, a1, b1, X[11], 14); e1 = ROL(e1, 10);
|
||||
b1 = GGG(b1, c1, d1, e1, a1, X[15], 6); d1 = ROL(d1, 10);
|
||||
a1 = GGG(a1, b1, c1, d1, e1, X[0], 14); c1 = ROL(c1, 10);
|
||||
e1 = GGG(e1, a1, b1, c1, d1, X[5], 6); b1 = ROL(b1, 10);
|
||||
d1 = GGG(d1, e1, a1, b1, c1, X[12], 9); a1 = ROL(a1, 10);
|
||||
c1 = GGG(c1, d1, e1, a1, b1, X[2], 12); e1 = ROL(e1, 10);
|
||||
b1 = GGG(b1, c1, d1, e1, a1, X[13], 9); d1 = ROL(d1, 10);
|
||||
a1 = GGG(a1, b1, c1, d1, e1, X[9], 12); c1 = ROL(c1, 10);
|
||||
e1 = GGG(e1, a1, b1, c1, d1, X[7], 5); b1 = ROL(b1, 10);
|
||||
d1 = GGG(d1, e1, a1, b1, c1, X[10], 15); a1 = ROL(a1, 10);
|
||||
c1 = GGG(c1, d1, e1, a1, b1, X[14], 8); e1 = ROL(e1, 10);
|
||||
|
||||
b1 = FFF(b1, c1, d1, e1, a1, X[12], 8); d1 = ROL(d1, 10);
|
||||
a1 = FFF(a1, b1, c1, d1, e1, X[15], 5); c1 = ROL(c1, 10);
|
||||
e1 = FFF(e1, a1, b1, c1, d1, X[10], 12); b1 = ROL(b1, 10);
|
||||
d1 = FFF(d1, e1, a1, b1, c1, X[4], 9); a1 = ROL(a1, 10);
|
||||
c1 = FFF(c1, d1, e1, a1, b1, X[1], 12); e1 = ROL(e1, 10);
|
||||
b1 = FFF(b1, c1, d1, e1, a1, X[5], 5); d1 = ROL(d1, 10);
|
||||
a1 = FFF(a1, b1, c1, d1, e1, X[8], 14); c1 = ROL(c1, 10);
|
||||
e1 = FFF(e1, a1, b1, c1, d1, X[7], 6); b1 = ROL(b1, 10);
|
||||
d1 = FFF(d1, e1, a1, b1, c1, X[0], 8); a1 = ROL(a1, 10);
|
||||
c1 = FFF(c1, d1, e1, a1, b1, X[6], 5); e1 = ROL(e1, 10);
|
||||
b1 = FFF(b1, c1, d1, e1, a1, X[11], 12); d1 = ROL(d1, 10);
|
||||
a1 = FFF(a1, b1, c1, d1, e1, X[13], 6); c1 = ROL(c1, 10);
|
||||
e1 = FFF(e1, a1, b1, c1, d1, X[2], 5); b1 = ROL(b1, 10);
|
||||
d1 = FFF(d1, e1, a1, b1, c1, X[14], 15); a1 = ROL(a1, 10);
|
||||
c1 = FFF(c1, d1, e1, a1, b1, X[9], 13); e1 = ROL(e1, 10);
|
||||
b1 = FFF(b1, c1, d1, e1, a1, X[3], 11); d1 = ROL(d1, 10);
|
||||
|
||||
var t = h1 + c + d1;
|
||||
h1 = h2 + d + e1;
|
||||
h2 = h3 + e + a1;
|
||||
h3 = h4 + a + b1;
|
||||
h4 = h0 + b + c1;
|
||||
h0 = t;
|
||||
}
|
||||
|
||||
var result = Bytes.alloc(20);
|
||||
result.set(0, h0 & 0xFF);
|
||||
result.set(1, (h0 >> 8) & 0xFF);
|
||||
result.set(2, (h0 >> 16) & 0xFF);
|
||||
result.set(3, (h0 >> 24) & 0xFF);
|
||||
result.set(4, h1 & 0xFF);
|
||||
result.set(5, (h1 >> 8) & 0xFF);
|
||||
result.set(6, (h1 >> 16) & 0xFF);
|
||||
result.set(7, (h1 >> 24) & 0xFF);
|
||||
result.set(8, h2 & 0xFF);
|
||||
result.set(9, (h2 >> 8) & 0xFF);
|
||||
result.set(10, (h2 >> 16) & 0xFF);
|
||||
result.set(11, (h2 >> 24) & 0xFF);
|
||||
result.set(12, h3 & 0xFF);
|
||||
result.set(13, (h3 >> 8) & 0xFF);
|
||||
result.set(14, (h3 >> 16) & 0xFF);
|
||||
result.set(15, (h3 >> 24) & 0xFF);
|
||||
result.set(16, h4 & 0xFF);
|
||||
result.set(17, (h4 >> 8) & 0xFF);
|
||||
result.set(18, (h4 >> 16) & 0xFF);
|
||||
result.set(19, (h4 >> 24) & 0xFF);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
969
leenkx/Sources/leenkx/network/torrent/Torrent.hx
Normal file
969
leenkx/Sources/leenkx/network/torrent/Torrent.hx
Normal file
@ -0,0 +1,969 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.peer.IPeerChannel;
|
||||
import leenkx.network.torrent.peer.WireProtocol;
|
||||
import leenkx.network.torrent.peer.RtcPeerPool;
|
||||
import leenkx.network.torrent.storage.Storage;
|
||||
import leenkx.network.torrent.storage.Storage.IStorage;
|
||||
|
||||
class Torrent {
|
||||
static inline var MAX_OUTSTANDING = 8;
|
||||
static inline var META_BLOCK = 16384;
|
||||
static inline var MAX_META_SIZE = 10000000;
|
||||
static inline var KEEPALIVE_MS = 60000;
|
||||
|
||||
public var meta:MetaInfo = null;
|
||||
public var infoHash:Bytes;
|
||||
public var infoHashHex:String;
|
||||
public var infoHashV2:Bytes = null;
|
||||
public var infoHashV2Hex:String = null;
|
||||
public var pendingWebSeeds:Array<String> = null;
|
||||
public var magnetURI:String = null;
|
||||
public var files:Array<TorrentFile> = [];
|
||||
public var ready = false;
|
||||
public var done = false;
|
||||
public var numPeers(get, never):Int;
|
||||
function get_numPeers():Int return Lambda.count(wires);
|
||||
|
||||
var listeners:Map<String, Array<Dynamic>> = [];
|
||||
public var onPeerExtension:String -> String -> Bytes -> Void;
|
||||
|
||||
public var uploadedBytes:Float = 0;
|
||||
public var stopped = false;
|
||||
public var client:TorrentClient = null;
|
||||
var webSeeds:Array<WebSeed> = [];
|
||||
var lastStatT:Float = 0;
|
||||
var lastStatDown:Float = 0;
|
||||
var lastStatUp:Float = 0;
|
||||
public var announcePort:Int = 0;
|
||||
public var downloadDir:String = null;
|
||||
|
||||
public function getStats():{downloaded:Float, uploaded:Float, left:Float, downRate:Float, upRate:Float} {
|
||||
var total = meta != null ? meta.totalLength : 0;
|
||||
var downloaded = pieces != null ? Math.min(pieces.numHave * (meta.pieceLength:Float), total) : 0;
|
||||
var now = haxe.Timer.stamp();
|
||||
var dt = now - lastStatT;
|
||||
var downRate:Float = 0;
|
||||
var upRate:Float = 0;
|
||||
if (lastStatT > 0 && dt > 0) {
|
||||
downRate = (downloaded - lastStatDown) / dt;
|
||||
upRate = (uploadedBytes - lastStatUp) / dt;
|
||||
if (downRate < 0) downRate = 0;
|
||||
if (upRate < 0) upRate = 0;
|
||||
}
|
||||
lastStatT = now;
|
||||
lastStatDown = downloaded;
|
||||
lastStatUp = uploadedBytes;
|
||||
return {
|
||||
downloaded: downloaded,
|
||||
uploaded: uploadedBytes,
|
||||
left: total - downloaded,
|
||||
downRate: downRate,
|
||||
upRate: upRate
|
||||
};
|
||||
}
|
||||
|
||||
var peerId:Bytes;
|
||||
var trackers:Array<String>;
|
||||
var storage:IStorage;
|
||||
public var trackerObjects:Array<Dynamic> = [];
|
||||
@:allow(leenkx.network.torrent.TorrentFile)
|
||||
@:allow(leenkx.network.torrent.WebSeed)
|
||||
var pieces:PieceManager = null;
|
||||
var wires:Map<String, WireProtocol> = [];
|
||||
var peerBitfields:Map<String, Array<Bool>> = [];
|
||||
var outstanding:Map<String, Int> = [];
|
||||
var pieceCursor:Map<String, Int> = [];
|
||||
var rtcPool:RtcPeerPool = null;
|
||||
var httpTrackers:Array<Dynamic> = [];
|
||||
var udpTrackers:Array<Dynamic> = [];
|
||||
var keepaliveTimer:haxe.Timer = null;
|
||||
var destroyed = false;
|
||||
|
||||
var metaSize:Int = 0;
|
||||
var metaReceived:Int = 0;
|
||||
var metaRequested:Array<Bool> = [];
|
||||
var metaRejects:Int = 0;
|
||||
var metaWire:WireProtocol = null;
|
||||
|
||||
public function new(infoHash:Bytes, peerId:Bytes, trackers:Array<String>, ?meta:MetaInfo, ?storage:IStorage, ?downloadDir:String) {
|
||||
TorrentClient.initMain();
|
||||
this.infoHash = infoHash;
|
||||
this.infoHashHex = infoHash != null ? Crypto.toHex(infoHash) : null;
|
||||
this.peerId = peerId;
|
||||
this.trackers = trackers;
|
||||
this.storage = storage;
|
||||
this.downloadDir = downloadDir;
|
||||
if (meta != null) setMeta(meta);
|
||||
}
|
||||
|
||||
public function on(event:String, cb:Dynamic):Void {
|
||||
var l = listeners.get(event);
|
||||
if (l == null) listeners.set(event, l = []);
|
||||
l.push(cb);
|
||||
}
|
||||
|
||||
function emit(event:String):Void {
|
||||
var l = listeners.get(event);
|
||||
if (l != null) for (cb in l) cb();
|
||||
}
|
||||
|
||||
function emit1(event:String, v:Dynamic):Void {
|
||||
var l = listeners.get(event);
|
||||
if (l != null) for (cb in l) cb(v);
|
||||
}
|
||||
|
||||
function setMeta(m:MetaInfo):Void {
|
||||
meta = m;
|
||||
if (storage == null) {
|
||||
var sz = m.storageLength > 0 ? m.storageLength : m.totalLength;
|
||||
#if (sys || kha_krom)
|
||||
storage = downloadDir != null ? new FileTreeStorage(downloadDir, m.files) : new MemoryStorage(sz);
|
||||
#else
|
||||
storage = new MemoryStorage(sz);
|
||||
#end
|
||||
}
|
||||
if (pieces == null) {
|
||||
pieces = new PieceManager(m, storage);
|
||||
pieces.onPieceComplete = onPieceComplete;
|
||||
pieces.onComplete = onTorrentComplete;
|
||||
}
|
||||
if (m.fileHashes == null && m.files != null) {
|
||||
m.fileHashes = [for (f in m.files) null];
|
||||
}
|
||||
files = [for (f in m.files) new TorrentFile(f, storage, this)];
|
||||
infoHashV2 = m.infoHashV2;
|
||||
infoHashV2Hex = m.infoHashV2Hex;
|
||||
if (infoHash == null && m.infoHash != null) {
|
||||
infoHash = m.infoHash;
|
||||
infoHashHex = m.infoHashHex;
|
||||
}
|
||||
if (infoHashHex == null) infoHashHex = m.infoHashV2Hex;
|
||||
if (m.urlList == null && pendingWebSeeds != null) {
|
||||
m.urlList = pendingWebSeeds;
|
||||
}
|
||||
magnetURI = Magnet.build(m.infoHash, m.name, trackers, m.urlList, m.infoHashV2);
|
||||
ready = true;
|
||||
initWebSeeds();
|
||||
emit1("metadata", m);
|
||||
emit("ready");
|
||||
if (!pieces.complete) {
|
||||
for (w in wires) w.sendInterested();
|
||||
}
|
||||
}
|
||||
|
||||
public function markComplete():Void {
|
||||
if (pieces == null) return;
|
||||
for (i in 0...meta.numPieces) pieces.markHave(i);
|
||||
done = true;
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
if (destroyed || keepaliveTimer != null) return;
|
||||
stopped = false;
|
||||
startTrackers();
|
||||
keepaliveTimer = new haxe.Timer(KEEPALIVE_MS);
|
||||
keepaliveTimer.run = function() {
|
||||
for (w in wires) w.sendKeepAlive();
|
||||
workWebSeeds();
|
||||
};
|
||||
workWebSeeds();
|
||||
}
|
||||
|
||||
public function stop():Void {
|
||||
if (destroyed || stopped) return;
|
||||
stopped = true;
|
||||
if (keepaliveTimer != null) {
|
||||
keepaliveTimer.stop();
|
||||
keepaliveTimer = null;
|
||||
}
|
||||
for (w in [for (x in wires) x]) w.close();
|
||||
wires.clear();
|
||||
outstanding.clear();
|
||||
if (rtcPool != null) {
|
||||
rtcPool.close();
|
||||
rtcPool = null;
|
||||
}
|
||||
for (t in httpTrackers) t.stop();
|
||||
for (t in udpTrackers) t.stop();
|
||||
httpTrackers = [];
|
||||
udpTrackers = [];
|
||||
trackerObjects = [];
|
||||
}
|
||||
|
||||
function startTrackers():Void {
|
||||
for (url in trackers) startTracker(url);
|
||||
}
|
||||
|
||||
public function addTracker(url:String):Dynamic {
|
||||
if (url == null || url == "") return null;
|
||||
for (t in trackers) {
|
||||
if (t == url) return null;
|
||||
}
|
||||
trackers.push(url);
|
||||
return startTracker(url);
|
||||
}
|
||||
|
||||
public function setTrackers(urls:Array<String>):Void {
|
||||
if (stopped) {
|
||||
trackers = urls != null ? urls.copy() : [];
|
||||
return;
|
||||
}
|
||||
if (rtcPool != null) {
|
||||
rtcPool.close();
|
||||
rtcPool = null;
|
||||
}
|
||||
for (t in httpTrackers) t.stop();
|
||||
for (t in udpTrackers) t.stop();
|
||||
httpTrackers = [];
|
||||
udpTrackers = [];
|
||||
trackerObjects = [];
|
||||
trackers = urls != null ? urls.copy() : [];
|
||||
startTrackers();
|
||||
}
|
||||
|
||||
function startTracker(url:String):Dynamic {
|
||||
var infoHashBin = TorrentInfo.toBinaryString(infoHash);
|
||||
var peerIdBin = TorrentInfo.toBinaryString(peerId);
|
||||
var obj:Dynamic = null;
|
||||
if (url.indexOf("ws") == 0) {
|
||||
if (rtcPool == null) {
|
||||
rtcPool = new RtcPeerPool();
|
||||
rtcPool.onPeerChannel = onRtcPeer;
|
||||
rtcPool.statsProvider = getStats;
|
||||
rtcPool.onError = function(e) {
|
||||
emit1("error", e);
|
||||
};
|
||||
}
|
||||
obj = rtcPool.connect(url, infoHashBin, peerIdBin);
|
||||
}
|
||||
#if (sys || kha_krom)
|
||||
else if (url.indexOf("http") == 0) {
|
||||
var t = new leenkx.network.torrent.tracker.HttpTracker(url, infoHash, peerId, announcePort > 0 ? announcePort : 6881);
|
||||
t.statsProvider = getStats;
|
||||
t.onPeers = onClassicPeers;
|
||||
t.onError = function(e) {
|
||||
emit1("error", e);
|
||||
};
|
||||
httpTrackers.push(t);
|
||||
t.start();
|
||||
obj = t;
|
||||
}
|
||||
#end
|
||||
#if (sys || kha_krom)
|
||||
else if (url.indexOf("udp") == 0) {
|
||||
var t = new leenkx.network.torrent.tracker.UdpTracker(url, infoHash, peerId, announcePort > 0 ? announcePort : 6881);
|
||||
t.statsProvider = getStats;
|
||||
t.onPeers = onClassicPeers;
|
||||
t.onError = function(e) {
|
||||
emit1("error", e);
|
||||
};
|
||||
udpTrackers.push(t);
|
||||
t.start();
|
||||
obj = t;
|
||||
}
|
||||
#end
|
||||
if (obj != null) trackerObjects.push(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
function onRtcPeer(peerIdHex:String, ch:IPeerChannel):Void {
|
||||
addWire("rtc:" + peerIdHex, ch);
|
||||
}
|
||||
|
||||
#if (sys || kha_krom)
|
||||
function onClassicPeers(list:Array<{host:String, port:Int}>):Void {
|
||||
for (p in list) {
|
||||
var key = "tcp:" + p.host + ":" + p.port;
|
||||
if (wires.exists(key)) continue;
|
||||
var ch = new leenkx.network.torrent.peer.TcpPeer(p.host, p.port);
|
||||
addWire(key, ch);
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
public function addIncomingPeer(key:String, ch:IPeerChannel):Void {
|
||||
addWire(key, ch);
|
||||
}
|
||||
|
||||
function addWire(key:String, ch:IPeerChannel):Void {
|
||||
if (destroyed || stopped || wires.exists(key)) {
|
||||
ch.close();
|
||||
return;
|
||||
}
|
||||
var wire = new WireProtocol(ch, infoHash, peerId);
|
||||
wires.set(key, wire);
|
||||
outstanding.set(key, 0);
|
||||
wire.metaSize = meta != null ? meta.rawInfo.length : 0;
|
||||
wire.onHandshake = function(remotePeerId) {
|
||||
if (ready) wire.sendBitfield(pieces.bitfield);
|
||||
};
|
||||
wire.onExtendedHandshake = function(hs) {
|
||||
if (meta == null && wire.extIds.exists("ut_metadata")) {
|
||||
requestMetadata(wire);
|
||||
}
|
||||
if (ready && !pieces.complete) wire.sendInterested();
|
||||
};
|
||||
wire.onUnchoke = function() requestBlocks(key);
|
||||
wire.onBitfield = function(bf) {
|
||||
peerBitfields.set(key, bitfieldToArray(bf));
|
||||
if (ready && !pieces.complete) wire.sendInterested();
|
||||
requestBlocks(key);
|
||||
};
|
||||
wire.onHave = function(index) {
|
||||
if (meta == null || index < 0 || index >= meta.numPieces) return;
|
||||
var bf = peerBitfields.get(key);
|
||||
if (bf != null && index < bf.length) bf[index] = true;
|
||||
requestBlocks(key);
|
||||
};
|
||||
wire.onPiece = function(index, begin, data) {
|
||||
var n = outstanding.get(key);
|
||||
if (n != null && n > 0) outstanding.set(key, n - 1);
|
||||
if (pieces != null && index >= 0 && index < meta.numPieces && pieces.addBlock(index, begin, data)) {
|
||||
broadcastHave(index);
|
||||
}
|
||||
requestBlocks(key);
|
||||
};
|
||||
wire.onRequest = function(index, begin, length) {
|
||||
if (wire.amChoking || pieces == null || index < 0 || index >= meta.numPieces || begin < 0 || length <= 0 || begin + length > meta.pieceSize(index)) return;
|
||||
if (!pieces.have[index]) return;
|
||||
var data = pieces.readBlock(index, begin, length);
|
||||
uploadedBytes += data.length;
|
||||
wire.sendPiece(index, begin, data);
|
||||
};
|
||||
wire.onChoke = function() {
|
||||
outstanding.set(key, 0);
|
||||
if (pieces != null) pieces.clearAllRequested(key);
|
||||
};
|
||||
wire.onInterested = function() wire.sendUnchoke();
|
||||
wire.onHashRequest = function(p) onHashRequest(wire, p);
|
||||
wire.onHashes = onHashesMsg;
|
||||
wire.onHashReject = onHashRejectMsg;
|
||||
wire.onExtended = function(name, payload) {
|
||||
if (name == "ut_metadata") {
|
||||
onUtMetadata(wire, payload);
|
||||
} else if (onPeerExtension != null) {
|
||||
onPeerExtension(key, name, payload);
|
||||
}
|
||||
};
|
||||
wire.onClose = function() {
|
||||
wires.remove(key);
|
||||
peerBitfields.remove(key);
|
||||
outstanding.remove(key);
|
||||
if (pieces != null) pieces.clearAllRequested(key);
|
||||
if (meta == null && metaSize > 0 && metaWire == wire) {
|
||||
resetMetaRequests();
|
||||
metaWire = null;
|
||||
for (k in wires.keys()) {
|
||||
var w = wires.get(k);
|
||||
if (w != wire && w.extIds.exists("ut_metadata")) {
|
||||
metaWire = w;
|
||||
sendMetaRequest(w, nextMetaPiece());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
wire.onError = function(e) {
|
||||
emit1("error", "wire " + key + ": " + e);
|
||||
};
|
||||
wire.start(["ut_metadata", "lx_channel"]);
|
||||
emit1("wire", wire);
|
||||
}
|
||||
|
||||
static function bytesEq(a:Bytes, b:Bytes):Bool {
|
||||
if (a == null || b == null || a.length != b.length) return false;
|
||||
for (i in 0...a.length) if (a.get(i) != b.get(i)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function bitfieldToArray(bf:Bytes):Array<Bool> {
|
||||
var n = meta != null ? meta.numPieces : bf.length * 8;
|
||||
var out:Array<Bool> = [];
|
||||
for (i in 0...n) {
|
||||
var byte = i >> 3;
|
||||
out.push(byte < bf.length && (bf.get(byte) & (0x80 >> (i & 7))) != 0);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function tryRequest(wire:WireProtocol, key:String, bf:Array<Bool>, i:Int):Bool {
|
||||
if (pieces.have[i] || i >= bf.length || !bf[i]) return false;
|
||||
if (meta.pieces == null && meta.fileHashes != null) {
|
||||
var fi = meta.fileForPiece(i);
|
||||
if (fi >= 0 && meta.fileHashes[fi] == null) {
|
||||
requestPieceLayer(wire, fi);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
var needed = pieces.neededBlocks(key, i);
|
||||
if (needed.length == 0) return false;
|
||||
var b = needed[0];
|
||||
wire.sendRequest(i, b.begin, b.length);
|
||||
pieces.markRequested(key, i, b.begin);
|
||||
return true;
|
||||
}
|
||||
|
||||
function requestBlocks(key:String):Void {
|
||||
if (pieces == null || pieces.complete) return;
|
||||
var expired = pieces.expireRequests(60);
|
||||
for (pk in expired.keys()) {
|
||||
var c = outstanding.get(pk);
|
||||
if (c != null) {
|
||||
c -= expired.get(pk);
|
||||
outstanding.set(pk, c < 0 ? 0 : c);
|
||||
}
|
||||
}
|
||||
var wire = wires.get(key);
|
||||
var bf = peerBitfields.get(key);
|
||||
if (wire == null || bf == null || wire.peerChoking) return;
|
||||
var n = outstanding.get(key);
|
||||
var cursor = pieceCursor.get(key);
|
||||
if (cursor == null) cursor = 0;
|
||||
var numPieces = meta.numPieces;
|
||||
while (n < MAX_OUTSTANDING) {
|
||||
var sent = false;
|
||||
for (sel in pieces.selections) {
|
||||
for (i in sel.start...sel.end + 1) {
|
||||
if (tryRequest(wire, key, bf, i)) {
|
||||
n++;
|
||||
sent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sent) break;
|
||||
}
|
||||
if (!sent) {
|
||||
for (j in 0...numPieces) {
|
||||
var i = (cursor + j) % numPieces;
|
||||
if (tryRequest(wire, key, bf, i)) {
|
||||
n++;
|
||||
sent = true;
|
||||
pieceCursor.set(key, (i + 1) % numPieces);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!sent) break;
|
||||
}
|
||||
outstanding.set(key, n);
|
||||
workWebSeeds();
|
||||
}
|
||||
|
||||
public function select(start:Int, end:Int, ?priority:Int, ?notify:Void -> Void):Void {
|
||||
if (pieces == null) return;
|
||||
pieces.select(start, end, priority, notify);
|
||||
for (key in wires.keys()) requestBlocks(key);
|
||||
workWebSeeds();
|
||||
}
|
||||
|
||||
public function deselect(start:Int, end:Int, ?priority:Int):Void {
|
||||
if (pieces == null) return;
|
||||
pieces.deselect(start, end, priority);
|
||||
}
|
||||
|
||||
public function critical(start:Int, end:Int):Void {
|
||||
select(start, end, 0x7fffffff);
|
||||
}
|
||||
|
||||
public function addPeer(peer:String):Bool {
|
||||
#if (sys || kha_krom)
|
||||
if (peer == null) return false;
|
||||
var i = peer.lastIndexOf(":");
|
||||
if (i < 0) return false;
|
||||
var host = peer.substr(0, i);
|
||||
var port = Std.parseInt(peer.substr(i + 1));
|
||||
if (host == "" || port == null) return false;
|
||||
var key = "tcp:" + host + ":" + port;
|
||||
if (wires.exists(key)) return false;
|
||||
addWire(key, new leenkx.network.torrent.peer.TcpPeer(host, port));
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#end
|
||||
}
|
||||
|
||||
function broadcastHave(index:Int):Void {
|
||||
for (w in wires) w.sendHave(index);
|
||||
}
|
||||
|
||||
public function sendExtension(key:String, name:String, payload:Bytes):Void {
|
||||
if (key != null) {
|
||||
var w = wires.get(key);
|
||||
if (w != null) w.sendExtended(name, payload);
|
||||
return;
|
||||
}
|
||||
for (w in wires) w.sendExtended(name, payload);
|
||||
}
|
||||
|
||||
function onPieceComplete(index:Int):Void {
|
||||
emit1("piece", index);
|
||||
}
|
||||
|
||||
var hashReqNext:Map<Int, Int> = [];
|
||||
var hashReqTime:Map<Int, Float> = [];
|
||||
var pendingLayers:Map<Int, {arr:Array<Bytes>, got:Int}> = [];
|
||||
|
||||
function pieceLayerBase():Int {
|
||||
return Std.int(Math.log(meta.pieceLength / 16384) / Math.log(2));
|
||||
}
|
||||
|
||||
function requestPieceLayer(wire:WireProtocol, fi:Int):Void {
|
||||
var f = meta.files[fi];
|
||||
var root = f.piecesRoot;
|
||||
if (root == null || f.length <= 0) return;
|
||||
var np = Std.int(Math.ceil(f.length / meta.pieceLength));
|
||||
var sent = hashReqNext.exists(fi) ? hashReqNext.get(fi) : 0;
|
||||
if (sent >= np) {
|
||||
var t = hashReqTime.exists(fi) ? hashReqTime.get(fi) : 0;
|
||||
if (haxe.Timer.stamp() - t < 60) return;
|
||||
sent = 0;
|
||||
}
|
||||
var chunk = np - sent;
|
||||
if (chunk > 512) chunk = 512;
|
||||
var len = 2;
|
||||
while (len < chunk) len <<= 1;
|
||||
wire.sendHashRequest(root, pieceLayerBase(), sent, len, 0);
|
||||
hashReqNext.set(fi, sent + len);
|
||||
hashReqTime.set(fi, haxe.Timer.stamp());
|
||||
}
|
||||
|
||||
function onHashesMsg(payload:Bytes):Void {
|
||||
if (meta == null || meta.fileHashes == null) return;
|
||||
if (payload.length < 80) return;
|
||||
var root = payload.sub(0, 32);
|
||||
var base = WireProtocol.readU32(payload, 32);
|
||||
var index = WireProtocol.readU32(payload, 36);
|
||||
var length = WireProtocol.readU32(payload, 40);
|
||||
var proofs = WireProtocol.readU32(payload, 44);
|
||||
if (base != pieceLayerBase()) return;
|
||||
var count = Std.int((payload.length - 48) / 32);
|
||||
if (count <= 0) return;
|
||||
for (fi in 0...meta.files.length) {
|
||||
var f = meta.files[fi];
|
||||
if (f.piecesRoot == null || !bytesEq(root, f.piecesRoot)) continue;
|
||||
if (meta.fileHashes[fi] != null) return;
|
||||
var np = Std.int(Math.ceil(f.length / meta.pieceLength));
|
||||
var pl = pendingLayers.get(fi);
|
||||
if (pl == null) {
|
||||
pl = {arr: [for (i in 0...np) null], got: 0};
|
||||
pendingLayers.set(fi, pl);
|
||||
}
|
||||
var n = count < length ? count : length;
|
||||
for (j in 0...n) {
|
||||
var slot = index + j;
|
||||
if (slot < np && pl.arr[slot] == null) {
|
||||
pl.arr[slot] = payload.sub(48 + j * 32, 32);
|
||||
pl.got++;
|
||||
}
|
||||
}
|
||||
if (pl.got >= np) {
|
||||
pendingLayers.remove(fi);
|
||||
if (meta.verifyLayer(fi, pl.arr)) {
|
||||
meta.fileHashes[fi] = pl.arr;
|
||||
pieces.flushPendingV2(fi);
|
||||
for (k in wires.keys()) requestBlocks(k);
|
||||
} else {
|
||||
trace('[Torrent] v2 layer REJECTED fi=' + fi);
|
||||
hashReqNext.remove(fi);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function onHashRejectMsg(payload:Bytes):Void {
|
||||
if (meta == null) return;
|
||||
var root = payload.sub(0, 32);
|
||||
for (fi in 0...meta.files.length) {
|
||||
var f = meta.files[fi];
|
||||
if (f.piecesRoot != null && bytesEq(root, f.piecesRoot)) {
|
||||
hashReqNext.remove(fi);
|
||||
pendingLayers.remove(fi);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var layerCache:Map<Int, Array<Bytes>> = [];
|
||||
var treeCache:Map<Int, Array<Array<Bytes>>> = [];
|
||||
|
||||
function hashLayerForFile(fi:Int):Array<Bytes> {
|
||||
var l = layerCache.get(fi);
|
||||
if (l != null) return l;
|
||||
var f = meta.files[fi];
|
||||
var np = Std.int(Math.ceil(f.length / meta.pieceLength));
|
||||
l = [];
|
||||
for (p in 0...np) {
|
||||
var size = Std.int(Math.min(meta.pieceLength, f.length - p * meta.pieceLength));
|
||||
l.push(meta.pieceRootV2(storage.read(f.offset + p * meta.pieceLength, size)));
|
||||
}
|
||||
layerCache.set(fi, l);
|
||||
return l;
|
||||
}
|
||||
|
||||
function treeLevelsFor(fi:Int):Array<Array<Bytes>> {
|
||||
var t = treeCache.get(fi);
|
||||
if (t != null) return t;
|
||||
var level = hashLayerForFile(fi);
|
||||
t = [level];
|
||||
var depth = 0;
|
||||
while (level.length > 1) {
|
||||
var zero = MetaInfo.layerZero(pieceLayerBase() + depth + 1);
|
||||
var next:Array<Bytes> = [];
|
||||
var i = 0;
|
||||
while (i < level.length) {
|
||||
var b = i + 1 < level.length ? level[i + 1] : zero;
|
||||
next.push(MetaInfo.hashPair(level[i], b));
|
||||
i += 2;
|
||||
}
|
||||
t.push(next);
|
||||
level = next;
|
||||
depth++;
|
||||
}
|
||||
if (t.length > 1) t.pop();
|
||||
treeCache.set(fi, t);
|
||||
return t;
|
||||
}
|
||||
|
||||
function onHashRequest(wire:WireProtocol, payload:Bytes):Void {
|
||||
var root = payload.sub(0, 32);
|
||||
var base = WireProtocol.readU32(payload, 32);
|
||||
var index = WireProtocol.readU32(payload, 36);
|
||||
var length = WireProtocol.readU32(payload, 40);
|
||||
var proofs = WireProtocol.readU32(payload, 44);
|
||||
var reject = function() wire.sendHashReject(root, base,
|
||||
index, length, proofs);
|
||||
if (meta == null || meta.pieces != null || storage == null
|
||||
|| length < 2 || length > 512
|
||||
|| (length & (length - 1)) != 0
|
||||
|| (length > 0 && index % length != 0)) {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
var pieceBase = pieceLayerBase();
|
||||
if (base != pieceBase) {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
for (fi in 0...meta.files.length) {
|
||||
var f = meta.files[fi];
|
||||
if (f.piecesRoot == null || f.length <= 0
|
||||
|| !bytesEq(root, f.piecesRoot)) continue;
|
||||
var layer = hashLayerForFile(fi);
|
||||
if (index >= layer.length) {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
var n = layer.length - index;
|
||||
if (n > length) n = length;
|
||||
var buf = new haxe.io.BytesBuffer();
|
||||
for (i in index...index + n) {
|
||||
buf.addBytes(layer[i], 0, 32);
|
||||
}
|
||||
var lg = 0;
|
||||
while ((1 << lg) < length) lg++;
|
||||
var tree = treeLevelsFor(fi);
|
||||
for (p in 0...(proofs - lg + 1)) {
|
||||
var lvl = lg + p;
|
||||
if (lvl >= tree.length) break;
|
||||
var sib = (index >> lvl) ^ 1;
|
||||
var h = sib < tree[lvl].length
|
||||
? tree[lvl][sib]
|
||||
: MetaInfo.layerZero(pieceBase + lvl);
|
||||
buf.addBytes(h, 0, 32);
|
||||
}
|
||||
wire.sendHashes(root, base, index, n,
|
||||
proofs, buf.getBytes());
|
||||
return;
|
||||
}
|
||||
reject();
|
||||
}
|
||||
|
||||
function onTorrentComplete():Void {
|
||||
done = true;
|
||||
storage.flush();
|
||||
#if sys
|
||||
if (downloadDir != null
|
||||
&& Std.isOfType(storage, MemoryStorage)) {
|
||||
writeToDisk(downloadDir);
|
||||
}
|
||||
#end
|
||||
for (t in trackerObjects) {
|
||||
var f:Dynamic = Reflect.field(t, "announceEvent");
|
||||
if (f != null) {
|
||||
try {
|
||||
Reflect.callMethod(t, f, ["completed"]);
|
||||
} catch(e:Dynamic) {}
|
||||
}
|
||||
}
|
||||
emit("done");
|
||||
if (client != null && client.onTorrentDone != null) {
|
||||
client.onTorrentDone(this);
|
||||
}
|
||||
}
|
||||
|
||||
#if sys
|
||||
function writeToDisk(dir:String):Void {
|
||||
for (f in files) {
|
||||
var p = dir + "/" + MetaInfo.sanitizePath(f.path);
|
||||
try {
|
||||
var i = Std.int(Math.max(p.lastIndexOf("/"),
|
||||
p.lastIndexOf("\\")));
|
||||
if (i > 0) {
|
||||
var d = p.substr(0, i);
|
||||
if (!sys.FileSystem.exists(d)) {
|
||||
sys.FileSystem.createDirectory(d);
|
||||
}
|
||||
}
|
||||
var out = sys.io.File.write(p, true);
|
||||
out.write(f.getBytes());
|
||||
out.close();
|
||||
} catch(e:Dynamic) {
|
||||
emit1("error", "write " + p + ": " + Std.string(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
function initWebSeeds():Void {
|
||||
if (meta == null || meta.urlList == null) return;
|
||||
for (u in meta.urlList) addWebSeed(u);
|
||||
}
|
||||
|
||||
public function addWebSeed(url:String):Void {
|
||||
if (url == null || url == "") return;
|
||||
for (w in webSeeds) if (w.url == url) return;
|
||||
var ws = new WebSeed(url, this);
|
||||
var self = this;
|
||||
ws.onError = function(e) {
|
||||
self.emit1("error", "webseed " + url + ": " + e);
|
||||
haxe.Timer.delay(self.workWebSeeds, 10000);
|
||||
};
|
||||
ws.onDone = workWebSeeds;
|
||||
webSeeds.push(ws);
|
||||
ws.work();
|
||||
}
|
||||
|
||||
function workWebSeeds():Void {
|
||||
if (destroyed || stopped || done) return;
|
||||
for (w in webSeeds) w.work();
|
||||
}
|
||||
|
||||
|
||||
function requestMetadata(wire:WireProtocol):Void {
|
||||
if (metaSize > 0) {
|
||||
var alive = false;
|
||||
for (w in wires) {
|
||||
if (w == metaWire) alive = true;
|
||||
}
|
||||
if (!alive) {
|
||||
resetMetaRequests();
|
||||
metaWire = wire;
|
||||
sendMetaRequest(wire, nextMetaPiece());
|
||||
}
|
||||
return;
|
||||
}
|
||||
var hs = wire.peerExtendedHandshake;
|
||||
metaWire = wire;
|
||||
var size:Dynamic = Reflect.field(hs, "metadata_size");
|
||||
if (size == null) return;
|
||||
var sizeF = Std.parseFloat(Std.string(size));
|
||||
if (Math.isNaN(sizeF) || sizeF <= 0 || sizeF > MAX_META_SIZE) {
|
||||
emit1("error", "invalid metadata_size " + size);
|
||||
return;
|
||||
}
|
||||
metaSize = Std.int(sizeF);
|
||||
metaRejects = 0;
|
||||
var count = Std.int(Math.ceil(metaSize / META_BLOCK));
|
||||
metaChunks.clear();
|
||||
metaReceived = 0;
|
||||
metaRequested = [for (i in 0...count) false];
|
||||
sendMetaRequest(wire, 0);
|
||||
}
|
||||
|
||||
function resetMetaRequests():Void {
|
||||
for (i in 0...metaRequested.length) {
|
||||
if (metaRequested[i] && !metaChunks.exists(i)) {
|
||||
metaRequested[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sendMetaRequest(wire:WireProtocol, piece:Int):Void {
|
||||
if (piece < 0 || piece >= metaRequested.length
|
||||
|| metaRequested[piece]) return;
|
||||
metaRequested[piece] = true;
|
||||
var req = Bencode.encode({msg_type: 0, piece: piece});
|
||||
wire.sendExtended("ut_metadata", req);
|
||||
}
|
||||
|
||||
function onUtMetadata(wire:WireProtocol, payload:Bytes):Void {
|
||||
var headerEnd = findDictEnd(payload);
|
||||
if (headerEnd < 0) return;
|
||||
var header:Dynamic;
|
||||
try {
|
||||
header = Bencode.decode(payload.sub(0, headerEnd));
|
||||
} catch(e:Dynamic) {
|
||||
return;
|
||||
}
|
||||
var msgType = Std.int(metaField(header, "msg_type"));
|
||||
var piece = Std.int(metaField(header, "piece"));
|
||||
|
||||
if (msgType == 0) {
|
||||
if (meta == null || piece < 0) {
|
||||
var rej = Bencode.encode({msg_type: 2, piece: piece});
|
||||
wire.sendExtended("ut_metadata", rej);
|
||||
return;
|
||||
}
|
||||
var startF = piece * (META_BLOCK : Float);
|
||||
if (startF >= meta.rawInfo.length) return;
|
||||
var start = Std.int(startF);
|
||||
var len = meta.rawInfo.length - start;
|
||||
if (len > META_BLOCK) len = META_BLOCK;
|
||||
var head = Bencode.encode({
|
||||
msg_type: 1, piece: piece,
|
||||
total_size: meta.rawInfo.length
|
||||
});
|
||||
var out = new haxe.io.BytesBuffer();
|
||||
out.addBytes(head, 0, head.length);
|
||||
out.addBytes(meta.rawInfo, start, len);
|
||||
wire.sendExtended("ut_metadata", out.getBytes());
|
||||
} else if (msgType == 1) {
|
||||
var total = metaField(header, "total_size");
|
||||
if (Math.isNaN(total) || total <= 0 || total > MAX_META_SIZE) {
|
||||
return;
|
||||
}
|
||||
var totalI = Std.int(total);
|
||||
if (metaSize == 0 || totalI != metaSize) {
|
||||
metaSize = totalI;
|
||||
metaRejects = 0;
|
||||
metaChunks.clear();
|
||||
metaReceived = 0;
|
||||
metaRequested = [
|
||||
for (i in 0...Std.int(Math.ceil(totalI / META_BLOCK)))
|
||||
false
|
||||
];
|
||||
}
|
||||
if (piece < 0 || piece >= metaRequested.length) return;
|
||||
var expected = metaSize - piece * META_BLOCK;
|
||||
if (expected > META_BLOCK) expected = META_BLOCK;
|
||||
var chunk = payload.sub(headerEnd, payload.length - headerEnd);
|
||||
if (chunk.length > expected) {
|
||||
chunk = chunk.sub(0, expected);
|
||||
}
|
||||
appendMetaChunk(piece, chunk);
|
||||
if (metaReceived >= metaSize) finishMetadata(wire);
|
||||
else sendMetaRequest(wire, nextMetaPiece());
|
||||
} else if (msgType == 2) {
|
||||
if (piece >= 0 && piece < metaRequested.length) {
|
||||
metaRequested[piece] = false;
|
||||
metaRejects++;
|
||||
if (metaRejects > 2 * metaRequested.length) {
|
||||
metaSize = 0;
|
||||
metaChunks.clear();
|
||||
emit1("error", "ut_metadata rejected by peers");
|
||||
} else {
|
||||
for (k in wires.keys()) {
|
||||
var w = wires.get(k);
|
||||
if (w != wire
|
||||
&& w.extIds.exists("ut_metadata")) {
|
||||
sendMetaRequest(w, piece);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var metaChunks:Map<Int, Bytes> = [];
|
||||
|
||||
function appendMetaChunk(piece:Int, chunk:Bytes):Void {
|
||||
if (!metaChunks.exists(piece)) {
|
||||
metaChunks.set(piece, chunk);
|
||||
metaReceived += chunk.length;
|
||||
}
|
||||
}
|
||||
|
||||
function nextMetaPiece():Int {
|
||||
for (i in 0...metaRequested.length) {
|
||||
if (!metaRequested[i]) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function finishMetadata(wire:WireProtocol):Void {
|
||||
var buf = new haxe.io.BytesBuffer();
|
||||
var count = Std.int(Math.ceil(metaSize / META_BLOCK));
|
||||
for (i in 0...count) {
|
||||
var c = metaChunks.get(i);
|
||||
if (c != null) buf.addBytes(c, 0, c.length);
|
||||
}
|
||||
var raw = buf.getBytes().sub(0, metaSize);
|
||||
var okMeta = false;
|
||||
var v1Hash:Bytes = null;
|
||||
var sha1 = haxe.crypto.Sha1.make(raw);
|
||||
if (infoHash != null && bytesEq(sha1, infoHash)) {
|
||||
okMeta = true;
|
||||
v1Hash = infoHash;
|
||||
}
|
||||
if (!okMeta) {
|
||||
var sha2 = haxe.crypto.Sha256.make(raw);
|
||||
if (infoHashV2 != null && bytesEq(sha2, infoHashV2)) {
|
||||
okMeta = true;
|
||||
} else if (infoHash != null
|
||||
&& bytesEq(sha2.sub(0, 20), infoHash)) {
|
||||
okMeta = true;
|
||||
}
|
||||
}
|
||||
if (!okMeta) {
|
||||
emit1("error", "ut_metadata hash mismatch");
|
||||
metaRejects += metaRequested.length;
|
||||
metaChunks.clear();
|
||||
metaReceived = 0;
|
||||
if (metaRejects > 2 * metaRequested.length) {
|
||||
metaSize = 0;
|
||||
return;
|
||||
}
|
||||
for (i in 0...metaRequested.length) metaRequested[i] = false;
|
||||
sendMetaRequest(wire, nextMetaPiece());
|
||||
return;
|
||||
}
|
||||
setMeta(MetaInfo.fromInfoDict(raw, v1Hash, trackers));
|
||||
metaChunks.clear();
|
||||
for (key in wires.keys()) requestBlocks(key);
|
||||
}
|
||||
|
||||
static function metaField(d:Dynamic, name:String):Float {
|
||||
var v = Reflect.field(d, name);
|
||||
if (v == null) return 0;
|
||||
return Std.parseFloat(Std.string(v));
|
||||
}
|
||||
|
||||
static function findDictEnd(data:Bytes):Int {
|
||||
if (data.length == 0 || data.get(0) != 'd'.code) return -1;
|
||||
var end = MetaInfo.skipValue(data, 0);
|
||||
return end <= data.length ? end : -1;
|
||||
}
|
||||
|
||||
public function destroy():Void {
|
||||
if (destroyed) return;
|
||||
destroyed = true;
|
||||
if (keepaliveTimer != null) {
|
||||
keepaliveTimer.stop();
|
||||
keepaliveTimer = null;
|
||||
}
|
||||
if (rtcPool != null) rtcPool.close();
|
||||
for (t in httpTrackers) t.stop();
|
||||
for (t in udpTrackers) t.stop();
|
||||
for (w in [for (x in wires) x]) w.close();
|
||||
wires.clear();
|
||||
if (storage != null) storage.close();
|
||||
}
|
||||
|
||||
public function deleteData():Void {
|
||||
if (storage != null) storage.deleteData();
|
||||
}
|
||||
}
|
||||
407
leenkx/Sources/leenkx/network/torrent/TorrentClient.hx
Normal file
407
leenkx/Sources/leenkx/network/torrent/TorrentClient.hx
Normal file
@ -0,0 +1,407 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.storage.Storage;
|
||||
import leenkx.network.torrent.storage.Storage.IStorage;
|
||||
|
||||
class TorrentClient {
|
||||
public static var defaultTrackers:Array<String> = [];
|
||||
|
||||
public static var defaultDownloadDir:String = null;
|
||||
public var downloadDir:String = null;
|
||||
|
||||
#if sys
|
||||
static var mainQueue:sys.thread.Deque<Void -> Void> = null;
|
||||
static var mainMutex:sys.thread.Mutex = null;
|
||||
static var mainTimer:haxe.Timer = null;
|
||||
#end
|
||||
|
||||
public static function initMain():Void {
|
||||
#if sys
|
||||
if (mainQueue != null) return;
|
||||
mainQueue = new sys.thread.Deque();
|
||||
mainMutex = new sys.thread.Mutex();
|
||||
#end
|
||||
}
|
||||
|
||||
public static function postMain(f:Void -> Void):Void {
|
||||
#if sys
|
||||
if (mainQueue == null) {
|
||||
f();
|
||||
return;
|
||||
}
|
||||
mainQueue.add(f);
|
||||
mainMutex.acquire();
|
||||
if (mainTimer == null) {
|
||||
mainTimer = new haxe.Timer(5);
|
||||
mainTimer.run = drainMain;
|
||||
}
|
||||
mainMutex.release();
|
||||
#else
|
||||
f();
|
||||
#end
|
||||
}
|
||||
|
||||
#if sys
|
||||
static function drainMain():Void {
|
||||
while (true) {
|
||||
mainMutex.acquire();
|
||||
var f = mainQueue.pop(false);
|
||||
if (f == null) {
|
||||
mainTimer.stop();
|
||||
mainTimer = null;
|
||||
mainMutex.release();
|
||||
return;
|
||||
}
|
||||
mainMutex.release();
|
||||
try {
|
||||
f();
|
||||
} catch(e:Dynamic) {}
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
public var torrents:Map<String, Torrent> = [];
|
||||
public var peerId:Bytes;
|
||||
|
||||
public var onError:String -> Void;
|
||||
public var onTorrentAdded:Torrent -> Void;
|
||||
public var onTorrentDone:Torrent -> Void;
|
||||
|
||||
public var listenPort:Int = 0;
|
||||
var tcpListener:Dynamic = null;
|
||||
|
||||
public function new() {
|
||||
initMain();
|
||||
peerId = TorrentInfo.peerId();
|
||||
}
|
||||
|
||||
public function listen(port:Int):Void {
|
||||
#if (sys || kha_krom)
|
||||
listenPort = port;
|
||||
tcpListener = new leenkx.network.torrent.peer.TcpListener(port);
|
||||
tcpListener.onPeer = function(ch, hex, key) {
|
||||
var t = torrents.get(hex);
|
||||
if (t == null) {
|
||||
for (cand in torrents) {
|
||||
if (cand.infoHashHex == hex) {
|
||||
t = cand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (t == null) {
|
||||
ch.close();
|
||||
return;
|
||||
}
|
||||
t.addIncomingPeer(key, ch);
|
||||
};
|
||||
tcpListener.onError = function(e) {
|
||||
if (onError != null) onError(e);
|
||||
};
|
||||
tcpListener.start();
|
||||
#end
|
||||
}
|
||||
|
||||
public function add(torrentId:Dynamic, ?opts:Dynamic, ?cb:Torrent -> Void):Torrent {
|
||||
if (opts != null && Reflect.isFunction(opts)) {
|
||||
cb = opts;
|
||||
opts = null;
|
||||
}
|
||||
var meta:MetaInfo = null;
|
||||
var infoHash:Bytes = null;
|
||||
var infoHashV2:Bytes = null;
|
||||
var webSeeds:Array<String> = null;
|
||||
var trackers = defaultTrackers.copy();
|
||||
var name:String = null;
|
||||
|
||||
if (Std.isOfType(torrentId, Bytes)) {
|
||||
meta = MetaInfo.fromTorrentBytes(torrentId);
|
||||
infoHash = meta.infoHash;
|
||||
infoHashV2 = meta.infoHashV2;
|
||||
mergeTrackers(trackers, meta.announce);
|
||||
} else {
|
||||
var id = Std.string(torrentId);
|
||||
var magnet = Magnet.parse(id);
|
||||
if (magnet != null) {
|
||||
infoHash = magnet.infoHash;
|
||||
infoHashV2 = magnet.infoHashV2;
|
||||
name = magnet.name;
|
||||
webSeeds = magnet.webSeeds;
|
||||
mergeTrackers(trackers, magnet.trackers);
|
||||
} else if (id.length == 40 || id.length == 64) {
|
||||
var bytes:Bytes = null;
|
||||
try {
|
||||
bytes = Bytes.ofHex(id);
|
||||
} catch(e:Dynamic) {}
|
||||
if (bytes == null) {
|
||||
if (onError != null) {
|
||||
onError("unrecognized torrent id: " + id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (bytes.length == 20) infoHash = bytes;
|
||||
else infoHashV2 = bytes;
|
||||
} else {
|
||||
try {
|
||||
meta = MetaInfo.fromTorrentBytes(Bytes.ofString(id));
|
||||
infoHash = meta.infoHash;
|
||||
infoHashV2 = meta.infoHashV2;
|
||||
mergeTrackers(trackers, meta.announce);
|
||||
} catch(e:Dynamic) {
|
||||
if (onError != null) {
|
||||
onError("unrecognized torrent id: " + id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (opts != null) {
|
||||
var ann:Array<String> = Reflect.field(opts, "announce");
|
||||
if (Reflect.field(opts, "announceOverride") == true) {
|
||||
trackers = ann != null ? ann : [];
|
||||
} else {
|
||||
mergeTrackers(trackers, ann);
|
||||
}
|
||||
}
|
||||
|
||||
var key = infoHash != null ? Crypto.toHex(infoHash)
|
||||
: (infoHashV2 != null ? "v2:" + Crypto.toHex(infoHashV2)
|
||||
: null);
|
||||
if (key == null) {
|
||||
if (onError != null) onError("unrecognized torrent id");
|
||||
return null;
|
||||
}
|
||||
var existing = torrents.get(key);
|
||||
if (existing != null) {
|
||||
if (cb != null) cb(existing);
|
||||
return existing;
|
||||
}
|
||||
|
||||
var wireHash = infoHash != null ? infoHash
|
||||
: infoHashV2.sub(0, 20);
|
||||
var t = new Torrent(wireHash, peerId, trackers, meta, null,
|
||||
downloadDir != null ? downloadDir : defaultDownloadDir);
|
||||
t.announcePort = listenPort > 0 ? listenPort
|
||||
: 49152 + Std.random(16384);
|
||||
t.infoHashV2 = infoHashV2;
|
||||
if (infoHashV2 != null) {
|
||||
t.infoHashV2Hex = Crypto.toHex(infoHashV2);
|
||||
}
|
||||
t.pendingWebSeeds = webSeeds;
|
||||
if (name != null && meta == null) t.magnetURI = Magnet.build(
|
||||
infoHash, name, trackers, webSeeds, infoHashV2);
|
||||
t.on("error", function(e) {
|
||||
if (onError != null) onError(e);
|
||||
});
|
||||
t.client = this;
|
||||
torrents.set(key, t);
|
||||
t.start();
|
||||
if (onTorrentAdded != null) onTorrentAdded(t);
|
||||
if (cb != null) cb(t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public function seed(data:Dynamic, ?opts:Dynamic, ?cb:Torrent -> Void):Torrent {
|
||||
if (opts != null && Reflect.isFunction(opts)) {
|
||||
cb = opts;
|
||||
opts = null;
|
||||
}
|
||||
var pair = buildPair(data, opts);
|
||||
return seedMeta(pair.meta, pair.files, opts, cb);
|
||||
}
|
||||
|
||||
public static function buildMeta(data:Dynamic, ?opts:Dynamic):MetaInfo {
|
||||
return buildPair(data, opts).meta;
|
||||
}
|
||||
|
||||
static function buildPair(data:Dynamic, ?opts:Dynamic):{
|
||||
meta:MetaInfo, files:Array<{path:String, data:Bytes}>} {
|
||||
var name = "leenkx";
|
||||
var trackers = defaultTrackers.copy();
|
||||
if (opts != null) {
|
||||
var n = Reflect.field(opts, "name");
|
||||
if (n != null) name = Std.string(n);
|
||||
var ann:Array<String> = Reflect.field(opts, "announce");
|
||||
mergeTrackers(trackers, ann);
|
||||
}
|
||||
|
||||
var files:Array<{path:String, data:Bytes}> = [];
|
||||
if (Std.isOfType(data, Bytes)) {
|
||||
files.push({path: name, data: data});
|
||||
} else if (Std.isOfType(data, Array)) {
|
||||
for (f in (data:Array<Dynamic>)) {
|
||||
var p = Reflect.field(f, "path");
|
||||
var d:Bytes = Reflect.field(f, "data");
|
||||
if (d == null && Reflect.field(f, "content") != null) {
|
||||
d = Reflect.field(f, "content");
|
||||
}
|
||||
if (p != null && d != null) {
|
||||
files.push({path: Std.string(p), data: d});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
files.push({path: name, data: Bytes.ofString(Std.string(data))});
|
||||
}
|
||||
|
||||
return {meta: MetaInfo.create(name, files, trackers, opts), files: files};
|
||||
}
|
||||
|
||||
public static function readFolder(path:String, ?base:String):Array<{path:String, data:Bytes}> {
|
||||
#if sys
|
||||
var root = path;
|
||||
while (root.length > 0 && (StringTools.endsWith(root, "/")
|
||||
|| StringTools.endsWith(root, "\\"))) {
|
||||
root = root.substr(0, root.length - 1);
|
||||
}
|
||||
if (base == null) {
|
||||
var i = Std.int(Math.max(root.lastIndexOf("/"), root.lastIndexOf("\\")));
|
||||
base = i >= 0 ? root.substr(i + 1) : root;
|
||||
}
|
||||
var files:Array<{path:String, data:Bytes}> = [];
|
||||
readFolderInto(root, base, files);
|
||||
return files;
|
||||
#else
|
||||
throw "readFolder requires a sys target";
|
||||
#end
|
||||
}
|
||||
|
||||
#if sys
|
||||
static function readFolderInto(dir:String, prefix:String, files:Array<{path:String, data:Bytes}>):Void {
|
||||
for (entry in sys.FileSystem.readDirectory(dir)) {
|
||||
var p = dir + "/" + entry;
|
||||
var rel = prefix + "/" + entry;
|
||||
if (sys.FileSystem.isDirectory(p)) {
|
||||
readFolderInto(p, rel, files);
|
||||
} else {
|
||||
files.push({path: rel, data: sys.io.File.getBytes(p)});
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
||||
public function seedMeta(meta:MetaInfo, files:Array<{path:String, data:Bytes}>, ?opts:Dynamic, ?cb:Torrent -> Void):Torrent {
|
||||
var trackers = meta.announce.copy();
|
||||
if (opts != null) {
|
||||
var ann:Array<String> = Reflect.field(opts, "announce");
|
||||
mergeTrackers(trackers, ann);
|
||||
}
|
||||
var ms = new MemoryStorage(meta.storageLength > 0
|
||||
? meta.storageLength : meta.totalLength);
|
||||
for (i in 0...files.length) {
|
||||
ms.write(meta.files[i].offset, files[i].data);
|
||||
}
|
||||
var storage:IStorage = ms;
|
||||
|
||||
var wireHash = meta.infoHash != null ? meta.infoHash
|
||||
: meta.infoHashV2.sub(0, 20);
|
||||
var t = new Torrent(wireHash, peerId, trackers, meta, storage);
|
||||
t.announcePort = listenPort > 0 ? listenPort
|
||||
: 49152 + Std.random(16384);
|
||||
t.markComplete();
|
||||
t.on("error", function(e) {
|
||||
if (onError != null) onError(e);
|
||||
});
|
||||
t.client = this;
|
||||
torrents.set(meta.infoHashHex != null ? meta.infoHashHex
|
||||
: "v2:" + meta.infoHashV2Hex, t);
|
||||
t.start();
|
||||
if (onTorrentAdded != null) onTorrentAdded(t);
|
||||
if (onTorrentDone != null) onTorrentDone(t);
|
||||
if (cb != null) cb(t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public function get(id:Dynamic):Torrent {
|
||||
if (Std.isOfType(id, Torrent)) return id;
|
||||
if (id == null) return null;
|
||||
var hex = Std.string(id);
|
||||
if (StringTools.startsWith(hex, "v2:")) {
|
||||
hex = hex.substr(3);
|
||||
}
|
||||
var t = torrents.get(hex);
|
||||
if (t == null) t = torrents.get("v2:" + hex);
|
||||
if (t == null) {
|
||||
for (f in torrents) {
|
||||
if (f.infoHashHex == hex || f.infoHashV2Hex == hex || (f.infoHash != null && Crypto.toHex(f.infoHash) == hex)) {
|
||||
t = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public static function torrentIdFor(v:Dynamic):Dynamic {
|
||||
if (v == null) return null;
|
||||
if (Std.isOfType(v, haxe.io.Bytes)) return v;
|
||||
var s = Std.string(v);
|
||||
if (s.indexOf("magnet:") == 0) return s;
|
||||
if (s.indexOf("v2:") == 0) {
|
||||
var h = s.substr(3);
|
||||
return h.length == 64 ? "magnet:?xt=urn:btmh:1220" + h : s;
|
||||
}
|
||||
switch (s.length) {
|
||||
case 40 | 32:
|
||||
return "magnet:?xt=urn:btih:" + s;
|
||||
case 64:
|
||||
return "magnet:?xt=urn:btmh:1220" + s;
|
||||
case 68:
|
||||
return "magnet:?xt=urn:btmh:" + s;
|
||||
default:
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
public static function trackerUrls(v:Dynamic):Array<String> {
|
||||
if (v == null) return null;
|
||||
var items:Array<Dynamic> = Std.isOfType(v, Array)
|
||||
? (v:Array<Dynamic>) : [v];
|
||||
var out:Array<String> = [];
|
||||
for (item in items) {
|
||||
var u:Dynamic = null;
|
||||
if (!Std.isOfType(item, String)) {
|
||||
try {
|
||||
u = Reflect.field(item, "url");
|
||||
} catch(e:Dynamic) {}
|
||||
}
|
||||
var s = u != null ? Std.string(u) : Std.string(item);
|
||||
if (s != "" && s != "null") out.push(s);
|
||||
}
|
||||
return out.length > 0 ? out : null;
|
||||
}
|
||||
|
||||
public function remove(infoHashHex:String, ?deleteData:Bool):Void {
|
||||
var t = get(infoHashHex);
|
||||
if (t != null) {
|
||||
t.destroy();
|
||||
if (deleteData) t.deleteData();
|
||||
var key:String = null;
|
||||
for (k in torrents.keys()) {
|
||||
if (torrents.get(k) == t) { key = k; break; }
|
||||
}
|
||||
if (key != null) torrents.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static function mergeTrackers(trackers:Array<String>, urls:Array<String>):Void {
|
||||
if (urls == null) return;
|
||||
for (u in urls) {
|
||||
if (u != null && Lambda.indexOf(trackers, u) < 0) {
|
||||
trackers.push(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy():Void {
|
||||
for (t in torrents) t.destroy();
|
||||
torrents.clear();
|
||||
#if (sys || kha_krom)
|
||||
if (tcpListener != null) {
|
||||
tcpListener.stop();
|
||||
tcpListener = null;
|
||||
}
|
||||
#end
|
||||
}
|
||||
}
|
||||
102
leenkx/Sources/leenkx/network/torrent/TorrentFile.hx
Normal file
102
leenkx/Sources/leenkx/network/torrent/TorrentFile.hx
Normal file
@ -0,0 +1,102 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import leenkx.network.torrent.MetaInfo.TorrentFileEntry;
|
||||
import leenkx.network.torrent.storage.Storage.IStorage;
|
||||
|
||||
class TorrentFile {
|
||||
public var name:String;
|
||||
public var path:String;
|
||||
public var length:Int;
|
||||
public var offset:Int;
|
||||
|
||||
var storage:IStorage;
|
||||
public var torrent:Torrent;
|
||||
|
||||
public function new(entry:TorrentFileEntry, storage:IStorage, ?torrent:Torrent) {
|
||||
this.path = entry.path;
|
||||
this.name = entry.path.split("/").pop();
|
||||
this.length = entry.length;
|
||||
this.offset = entry.offset;
|
||||
this.storage = storage;
|
||||
this.torrent = torrent;
|
||||
}
|
||||
|
||||
public var downloaded(get, never):Float;
|
||||
function get_downloaded():Float {
|
||||
if (torrent == null || torrent.pieces == null) return 0;
|
||||
var pl = torrent.meta.pieceLength;
|
||||
return Math.min(havePieces() * (pl:Float), length);
|
||||
}
|
||||
|
||||
public var progress(get, never):Float;
|
||||
function get_progress():Float {
|
||||
return length > 0 ? downloaded / length : 0;
|
||||
}
|
||||
|
||||
function havePieces():Int {
|
||||
var pl = torrent.meta.pieceLength;
|
||||
var first = Std.int(offset / pl);
|
||||
var last = length > 0
|
||||
? Std.int((offset + length - 1) / pl) : first;
|
||||
var n = 0;
|
||||
for (i in first...last + 1) {
|
||||
if (torrent.pieces.have[i]) n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public function select(?priority:Int):Void {
|
||||
if (torrent == null) return;
|
||||
torrent.select(offset, offset + length - 1, priority);
|
||||
}
|
||||
|
||||
public function deselect(?priority:Int):Void {
|
||||
if (torrent == null) return;
|
||||
torrent.deselect(offset, offset + length - 1, priority);
|
||||
}
|
||||
|
||||
public function critical():Void {
|
||||
if (torrent == null) return;
|
||||
torrent.critical(offset, offset + length - 1);
|
||||
}
|
||||
|
||||
public var type(get, never):String;
|
||||
function get_type():String {
|
||||
var i = name != null ? name.lastIndexOf(".") : -1;
|
||||
var ext = i >= 0 ? name.substr(i + 1).toLowerCase() : "";
|
||||
return switch (ext) {
|
||||
case "mp4" | "m4v": "video/mp4";
|
||||
case "webm": "video/webm";
|
||||
case "mkv": "video/x-matroska";
|
||||
case "mov": "video/quicktime";
|
||||
case "avi": "video/x-msvideo";
|
||||
case "mp3": "audio/mpeg";
|
||||
case "ogg": "audio/ogg";
|
||||
case "ogv": "video/ogg";
|
||||
case "flac": "audio/flac";
|
||||
case "wav": "audio/wav";
|
||||
case "aac": "audio/aac";
|
||||
case "jpg" | "jpeg": "image/jpeg";
|
||||
case "png": "image/png";
|
||||
case "gif": "image/gif";
|
||||
case "webp": "image/webp";
|
||||
case "svg": "image/svg+xml";
|
||||
case "txt" | "log" | "md": "text/plain";
|
||||
case "html" | "htm": "text/html";
|
||||
case "css": "text/css";
|
||||
case "json": "application/json";
|
||||
case "pdf": "application/pdf";
|
||||
case "zip": "application/zip";
|
||||
default: "application/octet-stream";
|
||||
}
|
||||
}
|
||||
|
||||
public function getBytes():Bytes {
|
||||
return storage.read(offset, length);
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return getBytes().toString();
|
||||
}
|
||||
}
|
||||
72
leenkx/Sources/leenkx/network/torrent/TorrentInfo.hx
Normal file
72
leenkx/Sources/leenkx/network/torrent/TorrentInfo.hx
Normal file
@ -0,0 +1,72 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class TorrentInfo {
|
||||
|
||||
public static inline var PEERID_PREFIX = "-LX0001-";
|
||||
public static inline var PEERID_LENGTH = 20;
|
||||
public static inline var HASH_LENGTH = 20;
|
||||
public static inline var MIN_PIECE_LENGTH = 16384;
|
||||
|
||||
public static function pieceLength(size:Int):Int {
|
||||
var scaled = size < 1024 ? 1.0 : size / 1024;
|
||||
var exp = Std.int(Math.log(scaled) / Math.log(2) + 0.5);
|
||||
if (exp < 0) exp = 0;
|
||||
if (exp > 30) exp = 30;
|
||||
var len = 1 << exp;
|
||||
return len > MIN_PIECE_LENGTH ? len : MIN_PIECE_LENGTH;
|
||||
}
|
||||
|
||||
public static function pieces(content:Bytes, plen:Int):Bytes {
|
||||
var count = content.length == 0 ? 0 : Math.ceil(content.length / plen);
|
||||
var out = Bytes.alloc(count * HASH_LENGTH);
|
||||
for (i in 0...count) {
|
||||
var off = i * plen;
|
||||
var len = content.length - off;
|
||||
if (len > plen) len = plen;
|
||||
var sha = haxe.crypto.Sha1.make(content.sub(off, len));
|
||||
out.blit(i * HASH_LENGTH, sha, 0, HASH_LENGTH);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public static function infoDict(name:String, content:Bytes):Dynamic {
|
||||
var plen = pieceLength(content.length);
|
||||
var info:Dynamic = {};
|
||||
Reflect.setField(info, "length", content.length);
|
||||
Reflect.setField(info, "name", name);
|
||||
Reflect.setField(info, "piece length", plen);
|
||||
Reflect.setField(info, "pieces", pieces(content, plen));
|
||||
return info;
|
||||
}
|
||||
|
||||
public static function infoHash(name:String, content:Bytes):Bytes {
|
||||
return haxe.crypto.Sha1.make(Bencode.encode(infoDict(name, content)));
|
||||
}
|
||||
|
||||
public static function identifierHash(identifier:String):Bytes {
|
||||
return infoHash(identifier, Bytes.ofString(identifier));
|
||||
}
|
||||
|
||||
public static function peerId():Bytes {
|
||||
var prefix = Bytes.ofString(PEERID_PREFIX);
|
||||
var random = Bytes.ofString(Crypto.toHex(Crypto.randomBytes(6)));
|
||||
var out = Bytes.alloc(PEERID_LENGTH);
|
||||
out.blit(0, prefix, 0, prefix.length);
|
||||
out.blit(prefix.length, random, 0, PEERID_LENGTH - prefix.length);
|
||||
return out;
|
||||
}
|
||||
|
||||
public static function toBinaryString(bytes:Bytes):String {
|
||||
var sb = new StringBuf();
|
||||
for (i in 0...bytes.length) sb.addChar(bytes.get(i));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static function fromBinaryString(str:String):Bytes {
|
||||
var bytes = Bytes.alloc(str.length);
|
||||
for (i in 0...str.length) bytes.set(i, str.charCodeAt(i) & 0xFF);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
733
leenkx/Sources/leenkx/network/torrent/TweetNaCl.hx
Normal file
733
leenkx/Sources/leenkx/network/torrent/TweetNaCl.hx
Normal file
@ -0,0 +1,733 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
class TweetNaCl {
|
||||
|
||||
static var _0:Array<Int> = [for (i in 0...16) 0];
|
||||
static var _9:Array<Int> = { var a = [for (i in 0...32) 0]; a[0] = 9; a; };
|
||||
static var sigma:Array<Int> = [101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107];
|
||||
static var minusp:Array<Int> = [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252];
|
||||
static var L:Array<Float> = [0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10];
|
||||
|
||||
static inline function gf(?init:Array<Float>):Array<Float> {
|
||||
var r = [for (i in 0...16) 0.0];
|
||||
if (init != null) for (i in 0...init.length) r[i] = init[i];
|
||||
return r;
|
||||
}
|
||||
|
||||
static var gf0 = gf();
|
||||
static var gf1 = gf([1]);
|
||||
static var _121665 = gf([0xdb41, 1]);
|
||||
static var D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]);
|
||||
static var D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]);
|
||||
static var X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]);
|
||||
static var Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]);
|
||||
static var I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
|
||||
|
||||
static var K:Array<Array<Int>> = [
|
||||
[0x428a2f98, 0xd728ae22], [0x71374491, 0x23ef65cd], [0xb5c0fbcf, 0xec4d3b2f], [0xe9b5dba5, 0x8189dbbc],
|
||||
[0x3956c25b, 0xf348b538], [0x59f111f1, 0xb605d019], [0x923f82a4, 0xaf194f9b], [0xab1c5ed5, 0xda6d8118],
|
||||
[0xd807aa98, 0xa3030242], [0x12835b01, 0x45706fbe], [0x243185be, 0x4ee4b28c], [0x550c7dc3, 0xd5ffb4e2],
|
||||
[0x72be5d74, 0xf27b896f], [0x80deb1fe, 0x3b1696b1], [0x9bdc06a7, 0x25c71235], [0xc19bf174, 0xcf692694],
|
||||
[0xe49b69c1, 0x9ef14ad2], [0xefbe4786, 0x384f25e3], [0x0fc19dc6, 0x8b8cd5b5], [0x240ca1cc, 0x77ac9c65],
|
||||
[0x2de92c6f, 0x592b0275], [0x4a7484aa, 0x6ea6e483], [0x5cb0a9dc, 0xbd41fbd4], [0x76f988da, 0x831153b5],
|
||||
[0x983e5152, 0xee66dfab], [0xa831c66d, 0x2db43210], [0xb00327c8, 0x98fb213f], [0xbf597fc7, 0xbeef0ee4],
|
||||
[0xc6e00bf3, 0x3da88fc2], [0xd5a79147, 0x930aa725], [0x06ca6351, 0xe003826f], [0x14292967, 0x0a0e6e70],
|
||||
[0x27b70a85, 0x46d22ffc], [0x2e1b2138, 0x5c26c926], [0x4d2c6dfc, 0x5ac42aed], [0x53380d13, 0x9d95b3df],
|
||||
[0x650a7354, 0x8baf63de], [0x766a0abb, 0x3c77b2a8], [0x81c2c92e, 0x47edaee6], [0x92722c85, 0x1482353b],
|
||||
[0xa2bfe8a1, 0x4cf10364], [0xa81a664b, 0xbc423001], [0xc24b8b70, 0xd0f89791], [0xc76c51a3, 0x0654be30],
|
||||
[0xd192e819, 0xd6ef5218], [0xd6990624, 0x5565a910], [0xf40e3585, 0x5771202a], [0x106aa070, 0x32bbd1b8],
|
||||
[0x19a4c116, 0xb8d2d0c8], [0x1e376c08, 0x5141ab53], [0x2748774c, 0xdf8eeb99], [0x34b0bcb5, 0xe19b48a8],
|
||||
[0x391c0cb3, 0xc5c95a63], [0x4ed8aa4a, 0xe3418acb], [0x5b9cca4f, 0x7763e373], [0x682e6ff3, 0xd6b2b8a3],
|
||||
[0x748f82ee, 0x5defb2fc], [0x78a5636f, 0x43172f60], [0x84c87814, 0xa1f0ab72], [0x8cc70208, 0x1a6439ec],
|
||||
[0x90befffa, 0x23631e28], [0xa4506ceb, 0xde82bde9], [0xbef9a3f7, 0xb2c67915], [0xc67178f2, 0xe372532b],
|
||||
[0xca273ece, 0xea26619c], [0xd186b8c7, 0x21c0c207], [0xeada7dd6, 0xcde0eb1e], [0xf57d4f7f, 0xee6ed178],
|
||||
[0x06f067aa, 0x72176fba], [0x0a637dc5, 0xa2c898a6], [0x113f9804, 0xbef90dae], [0x1b710b35, 0x131c471b],
|
||||
[0x28db77f5, 0x23047d84], [0x32caab7b, 0x40c72493], [0x3c9ebe0a, 0x15c9bebc], [0x431d67c4, 0x9c100d4c],
|
||||
[0x4cc5d4be, 0xcb3e42b6], [0x597f299c, 0xfc657e2a], [0x5fcb6fab, 0x3ad6faec], [0x6c44198c, 0x4a475817]
|
||||
];
|
||||
|
||||
static var iv:Array<Int> = [
|
||||
0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08,
|
||||
0xbb, 0x67, 0xae, 0x85, 0x84, 0xca, 0xa7, 0x3b,
|
||||
0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, 0xf8, 0x2b,
|
||||
0xa5, 0x4f, 0xf5, 0x3a, 0x5f, 0x1d, 0x36, 0xf1,
|
||||
0x51, 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1,
|
||||
0x9b, 0x05, 0x68, 0x8c, 0x2b, 0x3e, 0x6c, 0x1f,
|
||||
0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, 0x6b,
|
||||
0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79
|
||||
];
|
||||
|
||||
static inline function u64(h:Int, l:Int):Array<Int> { return [h, l]; }
|
||||
static inline function u64add(a:Array<Int>, b:Array<Int>):Array<Int> {
|
||||
var a0 = a[1] & 65535, a1 = a[1] >>> 16, a2 = a[0] & 65535, a3 = a[0] >>> 16;
|
||||
var b0 = b[1] & 65535, b1 = b[1] >>> 16, b2 = b[0] & 65535, b3 = b[0] >>> 16;
|
||||
var s0 = a0 + b0; var s1 = a1 + b1 + (s0 >>> 16);
|
||||
var s2 = a2 + b2 + (s1 >>> 16); var s3 = a3 + b3 + (s2 >>> 16);
|
||||
return [(s2 & 65535) | ((s3 & 65535) << 16), (s0 & 65535) | ((s1 & 65535) << 16)];
|
||||
}
|
||||
static function u64addMulti(args:Array<Array<Int>>):Array<Int> {
|
||||
var a0 = 0, a1 = 0, a2 = 0, a3 = 0;
|
||||
for (v in args) {
|
||||
a0 += v[1] & 65535; a1 += v[1] >>> 16;
|
||||
a2 += v[0] & 65535; a3 += v[0] >>> 16;
|
||||
}
|
||||
a1 += a0 >>> 16; a2 += a1 >>> 16; a3 += a2 >>> 16;
|
||||
return [(a2 & 65535) | ((a3 & 65535) << 16), (a0 & 65535) | ((a1 & 65535) << 16)];
|
||||
}
|
||||
static inline function u64xor(a:Array<Int>, b:Array<Int>):Array<Int> {
|
||||
return [a[0] ^ b[0], a[1] ^ b[1]];
|
||||
}
|
||||
static function u64xorMulti(args:Array<Array<Int>>):Array<Int> {
|
||||
var h = 0, l = 0;
|
||||
for (v in args) { h ^= v[0]; l ^= v[1]; }
|
||||
return [h, l];
|
||||
}
|
||||
static function u64R(x:Array<Int>, c:Int):Array<Int> {
|
||||
var h:Int, l:Int, c1 = 32 - c;
|
||||
if (c < 32) {
|
||||
h = (x[0] >>> c) | (x[1] << c1);
|
||||
l = (x[1] >>> c) | (x[0] << c1);
|
||||
} else {
|
||||
h = (x[1] >>> (c - 32)) | (x[0] << (64 - c));
|
||||
l = (x[0] >>> (c - 32)) | (x[1] << (64 - c));
|
||||
}
|
||||
return [h, l];
|
||||
}
|
||||
static inline function u64shr(x:Array<Int>, c:Int):Array<Int> {
|
||||
return [x[0] >>> c, (x[1] >>> c) | (x[0] << (32 - c))];
|
||||
}
|
||||
static inline function u64Ch(x:Array<Int>, y:Array<Int>, z:Array<Int>):Array<Int> {
|
||||
return [(x[0] & y[0]) ^ (~x[0] & z[0]), (x[1] & y[1]) ^ (~x[1] & z[1])];
|
||||
}
|
||||
static inline function u64Maj(x:Array<Int>, y:Array<Int>, z:Array<Int>):Array<Int> {
|
||||
return [(x[0] & y[0]) ^ (x[0] & z[0]) ^ (y[0] & z[0]), (x[1] & y[1]) ^ (x[1] & z[1]) ^ (y[1] & z[1])];
|
||||
}
|
||||
static inline function u64Sigma0(x:Array<Int>):Array<Int> { return u64xorMulti([u64R(x, 28), u64R(x, 34), u64R(x, 39)]); }
|
||||
static inline function u64Sigma1(x:Array<Int>):Array<Int> { return u64xorMulti([u64R(x, 14), u64R(x, 18), u64R(x, 41)]); }
|
||||
static inline function u64sigma0(x:Array<Int>):Array<Int> { return u64xorMulti([u64R(x, 1), u64R(x, 8), u64shr(x, 7)]); }
|
||||
static inline function u64sigma1(x:Array<Int>):Array<Int> { return u64xorMulti([u64R(x, 19), u64R(x, 61), u64shr(x, 6)]); }
|
||||
|
||||
static function dl64(x:Array<Int>, i:Int):Array<Int> {
|
||||
var h = (x[i] << 24) | (x[i + 1] << 16) | (x[i + 2] << 8) | x[i + 3];
|
||||
var l = (x[i + 4] << 24) | (x[i + 5] << 16) | (x[i + 6] << 8) | x[i + 7];
|
||||
return [h, l];
|
||||
}
|
||||
|
||||
static function ts64u(x:Array<Int>, i:Int, v:Array<Int>):Void {
|
||||
x[i] = (v[0] >> 24) & 0xff;
|
||||
x[i + 1] = (v[0] >> 16) & 0xff;
|
||||
x[i + 2] = (v[0] >> 8) & 0xff;
|
||||
x[i + 3] = v[0] & 0xff;
|
||||
x[i + 4] = (v[1] >> 24) & 0xff;
|
||||
x[i + 5] = (v[1] >> 16) & 0xff;
|
||||
x[i + 6] = (v[1] >> 8) & 0xff;
|
||||
x[i + 7] = v[1] & 0xff;
|
||||
}
|
||||
|
||||
static function L32(x:Int, c:Int):Int { return (x << c) | (x >>> (32 - c)); }
|
||||
|
||||
static function ld32(x:Array<Int>, i:Int):Int {
|
||||
var u = x[i + 3] & 0xff;
|
||||
u = (u << 8) | (x[i + 2] & 0xff);
|
||||
u = (u << 8) | (x[i + 1] & 0xff);
|
||||
return (u << 8) | (x[i + 0] & 0xff);
|
||||
}
|
||||
|
||||
static function st32(x:Array<Int>, j:Int, u:Int):Void {
|
||||
for (i in 0...4) { x[j + i] = u & 255; u = u >>> 8; }
|
||||
}
|
||||
|
||||
static function vn(x:Array<Int>, xi:Int, y:Array<Int>, yi:Int, n:Int):Int {
|
||||
var d = 0;
|
||||
for (i in 0...n) d |= x[xi + i] ^ y[yi + i];
|
||||
return (1 & ((d - 1) >>> 8)) - 1;
|
||||
}
|
||||
|
||||
static function crypto_verify_16(x:Array<Int>, xi:Int, y:Array<Int>, yi:Int):Int { return vn(x, xi, y, yi, 16); }
|
||||
static function crypto_verify_32(x:Array<Int>, xi:Int, y:Array<Int>, yi:Int):Int { return vn(x, xi, y, yi, 32); }
|
||||
|
||||
static function core(out:Array<Int>, inp:Array<Int>, k:Array<Int>, c:Array<Int>, h:Bool):Void {
|
||||
var w = [for (i in 0...16) 0], x = [for (i in 0...16) 0], y = [for (i in 0...16) 0], t = [for (i in 0...4) 0];
|
||||
for (i in 0...4) {
|
||||
x[5 * i] = ld32(c, 4 * i); x[1 + i] = ld32(k, 4 * i);
|
||||
x[6 + i] = ld32(inp, 4 * i); x[11 + i] = ld32(k, 16 + 4 * i);
|
||||
}
|
||||
for (i in 0...16) y[i] = x[i];
|
||||
for (i in 0...20) {
|
||||
for (j in 0...4) {
|
||||
for (m in 0...4) t[m] = x[(5 * j + 4 * m) % 16];
|
||||
t[1] ^= L32(t[0] + t[3], 7); t[2] ^= L32(t[1] + t[0], 9);
|
||||
t[3] ^= L32(t[2] + t[1], 13); t[0] ^= L32(t[3] + t[2], 18);
|
||||
for (m in 0...4) w[4 * j + (j + m) % 4] = t[m];
|
||||
}
|
||||
for (m in 0...16) x[m] = w[m];
|
||||
}
|
||||
if (h) {
|
||||
for (i in 0...16) x[i] += y[i];
|
||||
for (i in 0...4) { x[5 * i] -= ld32(c, 4 * i); x[6 + i] -= ld32(inp, 4 * i); }
|
||||
for (i in 0...4) { st32(out, 4 * i, x[5 * i]); st32(out, 16 + 4 * i, x[6 + i]); }
|
||||
} else {
|
||||
for (i in 0...16) st32(out, 4 * i, x[i] + y[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static function crypto_stream_salsa20_xor(c:Array<Int>, cpos:Int, m:Array<Int>, mpos:Int, b:Int, n:Array<Int>, k:Array<Int>):Int {
|
||||
var z = [for (i in 0...16) 0], x = [for (i in 0...64) 0];
|
||||
if (b == 0) return 0;
|
||||
for (i in 0...8) z[i] = n[i];
|
||||
while (b >= 64) {
|
||||
crypto_core_salsa20(x, z, k, sigma);
|
||||
for (i in 0...64) c[cpos + i] = (m != null ? m[mpos + i] : 0) ^ x[i];
|
||||
var u = 1;
|
||||
for (i in 8...16) { u += z[i]; z[i] = u & 0xff; u >>>= 8; }
|
||||
b -= 64; cpos += 64; if (m != null) mpos += 64;
|
||||
}
|
||||
if (b > 0) {
|
||||
crypto_core_salsa20(x, z, k, sigma);
|
||||
for (i in 0...b) c[cpos + i] = (m != null ? m[mpos + i] : 0) ^ x[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function crypto_stream(c:Array<Int>, cpos:Int, d:Int, n:Array<Int>, k:Array<Int>):Int {
|
||||
var np = [for (i in 0...32) 0];
|
||||
for (i in 0...n.length) np[i] = n[i];
|
||||
var s = [for (i in 0...32) 0];
|
||||
crypto_core_hsalsa20(s, np, k, sigma);
|
||||
var n2 = [for (i in 0...16) 0];
|
||||
for (i in 0...16) n2[i] = np[16 + i];
|
||||
return crypto_stream_salsa20(c, cpos, d, n2, s);
|
||||
}
|
||||
|
||||
static function crypto_stream_xor(c:Array<Int>, cpos:Int, m:Array<Int>, mpos:Int, d:Int, n:Array<Int>, k:Array<Int>):Int {
|
||||
var np = [for (i in 0...32) 0];
|
||||
for (i in 0...n.length) np[i] = n[i];
|
||||
var s = [for (i in 0...32) 0];
|
||||
crypto_core_hsalsa20(s, np, k, sigma);
|
||||
var n2 = [for (i in 0...16) 0];
|
||||
for (i in 0...16) n2[i] = np[16 + i];
|
||||
return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, n2, s);
|
||||
}
|
||||
|
||||
static inline function crypto_core_salsa20(out:Array<Int>, inp:Array<Int>, k:Array<Int>, c:Array<Int>):Int { core(out, inp, k, c, false); return 0; }
|
||||
static inline function crypto_core_hsalsa20(out:Array<Int>, inp:Array<Int>, k:Array<Int>, c:Array<Int>):Int { core(out, inp, k, c, true); return 0; }
|
||||
static inline function crypto_stream_salsa20(c:Array<Int>, cpos:Int, d:Int, n:Array<Int>, k:Array<Int>):Int { return crypto_stream_salsa20_xor(c, cpos, null, 0, d, n, k); }
|
||||
|
||||
static function add1305(h:Array<Int>, c:Array<Int>):Void {
|
||||
var u = 0;
|
||||
for (j in 0...17) { u += h[j] + c[j]; h[j] = u & 255; u >>>= 8; }
|
||||
}
|
||||
|
||||
static function crypto_onetimeauth(out:Array<Int>, outpos:Int, m:Array<Int>, mpos:Int, n:Int, k:Array<Int>):Int {
|
||||
var s:Int, i:Int, j:Int, u:Int;
|
||||
var x = [for (i in 0...17) 0], r = [for (i in 0...17) 0], h = [for (i in 0...17) 0], c = [for (i in 0...17) 0], g = [for (i in 0...17) 0];
|
||||
for (j in 0...17) r[j] = h[j] = 0;
|
||||
for (j in 0...16) r[j] = k[j];
|
||||
r[3] &= 15; r[4] &= 252; r[7] &= 15; r[8] &= 252; r[11] &= 15; r[12] &= 252; r[15] &= 15;
|
||||
while (n > 0) {
|
||||
for (j in 0...17) c[j] = 0;
|
||||
j = 0; while (j < 16 && j < n) { c[j] = m[mpos + j]; j++; }
|
||||
c[j] = 1; mpos += j; n -= j;
|
||||
add1305(h, c);
|
||||
for (i in 0...17) {
|
||||
x[i] = 0;
|
||||
for (j in 0...17) x[i] += Std.int(h[j] * ((j <= i) ? r[i - j] : (320 * r[i + 17 - j])));
|
||||
}
|
||||
for (i in 0...17) h[i] = x[i];
|
||||
u = 0;
|
||||
for (j in 0...16) { u += h[j]; h[j] = u & 255; u >>>= 8; }
|
||||
u += h[16]; h[16] = u & 3; u = 5 * (u >>> 2);
|
||||
for (j in 0...16) { u += h[j]; h[j] = u & 255; u >>>= 8; }
|
||||
u += h[16]; h[16] = u;
|
||||
}
|
||||
for (j in 0...17) g[j] = h[j];
|
||||
add1305(h, minusp);
|
||||
s = -(h[16] >>> 7);
|
||||
for (j in 0...17) h[j] ^= s & (g[j] ^ h[j]);
|
||||
for (j in 0...16) c[j] = k[j + 16];
|
||||
c[16] = 0; add1305(h, c);
|
||||
for (j in 0...16) out[outpos + j] = h[j];
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function crypto_secretbox(c:Array<Int>, m:Array<Int>, d:Int, n:Array<Int>, k:Array<Int>):Int {
|
||||
if (d < 32) return -1;
|
||||
crypto_stream_xor(c, 0, m, 0, d, n, k);
|
||||
crypto_onetimeauth(c, 16, c, 32, d - 32, c);
|
||||
for (i in 0...16) c[i] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function crypto_secretbox_open(m:Array<Int>, c:Array<Int>, d:Int, n:Array<Int>, k:Array<Int>):Int {
|
||||
var x = [for (i in 0...32) 0];
|
||||
if (d < 32) return -1;
|
||||
crypto_stream(x, 0, 32, n, k);
|
||||
if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) != 0) return -1;
|
||||
crypto_stream_xor(m, 0, c, 0, d, n, k);
|
||||
for (i in 0...32) m[i] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline function crypto_onetimeauth_verify(h:Array<Int>, hpos:Int, m:Array<Int>, mpos:Int, n:Int, k:Array<Int>):Int {
|
||||
var x = [for (i in 0...16) 0];
|
||||
crypto_onetimeauth(x, 0, m, mpos, n, k);
|
||||
return crypto_verify_16(h, hpos, x, 0);
|
||||
}
|
||||
|
||||
static function set25519(r:Array<Float>, a:Array<Float>):Void { for (i in 0...16) r[i] = a[i]; }
|
||||
|
||||
static function car25519(o:Array<Float>):Void {
|
||||
var c:Float;
|
||||
for (i in 0...16) {
|
||||
o[i] += 65536;
|
||||
c = Math.floor(o[i] / 65536);
|
||||
o[(i + 1) * (i < 15 ? 1 : 0)] += c - 1 + 37 * (c - 1) * (i == 15 ? 1 : 0);
|
||||
o[i] -= c * 65536;
|
||||
}
|
||||
}
|
||||
|
||||
static function sel25519(p:Array<Float>, q:Array<Float>, b:Int):Void {
|
||||
var c = ~((b - 1));
|
||||
for (i in 0...16) {
|
||||
var t:Int = c & (Std.int(p[i]) ^ Std.int(q[i]));
|
||||
p[i] = Std.int(p[i]) ^ t; q[i] = Std.int(q[i]) ^ t;
|
||||
}
|
||||
}
|
||||
|
||||
static function pack25519(o:Array<Int>, n:Array<Float>):Void {
|
||||
var m = gf(), t = gf();
|
||||
for (i in 0...16) t[i] = n[i];
|
||||
car25519(t); car25519(t); car25519(t);
|
||||
for (j in 0...2) {
|
||||
m[0] = t[0] - 0xffed;
|
||||
for (i in 1...15) {
|
||||
m[i] = t[i] - 0xffff - ((Std.int(m[i - 1]) >> 16) & 1);
|
||||
m[i - 1] = Std.int(m[i - 1]) & 0xffff;
|
||||
}
|
||||
m[15] = t[15] - 0x7fff - ((Std.int(m[14]) >> 16) & 1);
|
||||
var b = (Std.int(m[15]) >> 16) & 1;
|
||||
m[14] = Std.int(m[14]) & 0xffff;
|
||||
sel25519(t, m, 1 - b);
|
||||
}
|
||||
for (i in 0...16) {
|
||||
o[2 * i] = Std.int(t[i]) & 0xff;
|
||||
o[2 * i + 1] = Std.int(t[i]) >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
static function neq25519(a:Array<Float>, b:Array<Float>):Int {
|
||||
var c = [for (i in 0...32) 0], d = [for (i in 0...32) 0];
|
||||
pack25519(c, a); pack25519(d, b);
|
||||
return crypto_verify_32(c, 0, d, 0);
|
||||
}
|
||||
|
||||
static function par25519(a:Array<Float>):Int {
|
||||
var d = [for (i in 0...32) 0];
|
||||
pack25519(d, a);
|
||||
return d[0] & 1;
|
||||
}
|
||||
|
||||
static function unpack25519(o:Array<Float>, n:Array<Int>):Void {
|
||||
for (i in 0...16) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
|
||||
o[15] = Std.int(o[15]) & 0x7fff;
|
||||
}
|
||||
|
||||
static function A(o:Array<Float>, a:Array<Float>, b:Array<Float>):Void { for (i in 0...16) o[i] = a[i] + b[i]; }
|
||||
static function Z(o:Array<Float>, a:Array<Float>, b:Array<Float>):Void { for (i in 0...16) o[i] = a[i] - b[i]; }
|
||||
|
||||
static function M(o:Array<Float>, a:Array<Float>, b:Array<Float>):Void {
|
||||
var t = [for (i in 0...31) 0.0];
|
||||
for (i in 0...16) for (j in 0...16) t[i + j] += a[i] * b[j];
|
||||
for (i in 0...15) t[i] += 38 * t[i + 16];
|
||||
for (i in 0...16) o[i] = t[i];
|
||||
car25519(o); car25519(o);
|
||||
}
|
||||
|
||||
static inline function S(o:Array<Float>, a:Array<Float>):Void { M(o, a, a); }
|
||||
|
||||
static function inv25519(o:Array<Float>, i:Array<Float>):Void {
|
||||
var c = gf();
|
||||
for (a in 0...16) c[a] = i[a];
|
||||
var a = 253; while (a >= 0) {
|
||||
S(c, c);
|
||||
if (a != 2 && a != 4) M(c, c, i);
|
||||
a--;
|
||||
}
|
||||
for (a in 0...16) o[a] = c[a];
|
||||
}
|
||||
|
||||
static function pow2523(o:Array<Float>, i:Array<Float>):Void {
|
||||
var c = gf();
|
||||
for (a in 0...16) c[a] = i[a];
|
||||
var a = 250; while (a >= 0) {
|
||||
S(c, c);
|
||||
if (a != 1) M(c, c, i);
|
||||
a--;
|
||||
}
|
||||
for (a in 0...16) o[a] = c[a];
|
||||
}
|
||||
|
||||
static function crypto_scalarmult(q:Array<Int>, n:Array<Int>, p:Array<Int>):Int {
|
||||
var z = [for (i in 0...32) 0];
|
||||
var x = [for (i in 0...80) 0.0];
|
||||
var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
|
||||
for (i in 0...31) z[i] = n[i];
|
||||
z[31] = (n[31] & 127) | 64;
|
||||
z[0] &= 248;
|
||||
unpack25519(x, p);
|
||||
for (i in 0...16) { b[i] = x[i]; d[i] = a[i] = c[i] = 0; }
|
||||
a[0] = d[0] = 1;
|
||||
var r:Int;
|
||||
var i = 254; while (i >= 0) {
|
||||
r = (z[i >>> 3] >>> (i & 7)) & 1;
|
||||
sel25519(a, b, r); sel25519(c, d, r);
|
||||
A(e, a, c); Z(a, a, c); A(c, b, d); Z(b, b, d);
|
||||
S(d, e); S(f, a); M(a, c, a); M(c, b, e);
|
||||
A(e, a, c); Z(a, a, c); S(b, a); Z(c, d, f);
|
||||
M(a, c, _121665); A(a, a, d); M(c, c, a); M(a, d, f); M(d, b, x);
|
||||
S(b, e); sel25519(a, b, r); sel25519(c, d, r);
|
||||
i--;
|
||||
}
|
||||
for (i in 0...16) {
|
||||
x[i + 16] = a[i]; x[i + 32] = c[i]; x[i + 48] = b[i]; x[i + 64] = d[i];
|
||||
}
|
||||
var x32 = [for (i in 0...16) 0.0]; for (i in 0...16) x32[i] = x[i + 32];
|
||||
var x16 = [for (i in 0...16) 0.0]; for (i in 0...16) x16[i] = x[i + 16];
|
||||
inv25519(x32, x32);
|
||||
M(x16, x16, x32);
|
||||
pack25519(q, x16);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function crypto_scalarmult_base(q:Array<Int>, n:Array<Int>):Int { return crypto_scalarmult(q, n, _9); }
|
||||
|
||||
static function crypto_box_beforenm(k:Array<Int>, y:Array<Int>, x:Array<Int>):Int {
|
||||
var s = [for (i in 0...32) 0];
|
||||
crypto_scalarmult(s, x, y);
|
||||
return crypto_core_hsalsa20(k, _0, s, sigma);
|
||||
}
|
||||
|
||||
static function crypto_box(c:Array<Int>, m:Array<Int>, d:Int, n:Array<Int>, y:Array<Int>, x:Array<Int>):Int {
|
||||
var k = [for (i in 0...32) 0];
|
||||
crypto_box_beforenm(k, y, x);
|
||||
return crypto_secretbox(c, m, d, n, k);
|
||||
}
|
||||
|
||||
static function crypto_box_open(m:Array<Int>, c:Array<Int>, d:Int, n:Array<Int>, y:Array<Int>, x:Array<Int>):Int {
|
||||
var k = [for (i in 0...32) 0];
|
||||
crypto_box_beforenm(k, y, x);
|
||||
return crypto_secretbox_open(m, c, d, n, k);
|
||||
}
|
||||
|
||||
static function crypto_box_keypair(y:Array<Int>, x:Array<Int>, randomFn:Int->Array<Int>):Int {
|
||||
var rb = randomFn(32);
|
||||
for (i in 0...32) x[i] = rb[i];
|
||||
return crypto_scalarmult_base(y, x);
|
||||
}
|
||||
|
||||
static function crypto_hashblocks(x:Array<Int>, m:Array<Int>, mpos:Int, n:Int):Int {
|
||||
var z = [for (i in 0...8) null], b = [for (i in 0...8) null], a = [for (i in 0...8) null], w = [for (i in 0...16) null];
|
||||
for (i in 0...8) z[i] = a[i] = dl64(x, 8 * i);
|
||||
while (n >= 128) {
|
||||
for (i in 0...16) w[i] = dl64(m, 8 * i + mpos);
|
||||
for (i in 0...80) {
|
||||
for (j in 0...8) b[j] = a[j];
|
||||
var t = u64addMulti([a[7], u64Sigma1(a[4]), u64Ch(a[4], a[5], a[6]), u64(K[i][0], K[i][1]), w[i % 16]]);
|
||||
b[7] = u64addMulti([t, u64Sigma0(a[0]), u64Maj(a[0], a[1], a[2])]);
|
||||
b[3] = u64add(b[3], t);
|
||||
for (j in 0...8) a[(j + 1) % 8] = b[j];
|
||||
if (i % 16 == 15) {
|
||||
for (j in 0...16) w[j] = u64addMulti([w[j], w[(j + 9) % 16], u64sigma0(w[(j + 1) % 16]), u64sigma1(w[(j + 14) % 16])]);
|
||||
}
|
||||
}
|
||||
for (i in 0...8) { a[i] = u64add(a[i], z[i]); z[i] = a[i]; }
|
||||
mpos += 128; n -= 128;
|
||||
}
|
||||
for (i in 0...8) ts64u(x, 8 * i, z[i]);
|
||||
return n;
|
||||
}
|
||||
|
||||
static function crypto_hash(out:Array<Int>, m:Array<Int>, n:Int):Int {
|
||||
var h = [for (i in 0...64) 0], x = [for (i in 0...256) 0];
|
||||
var b = n;
|
||||
for (i in 0...64) h[i] = iv[i];
|
||||
crypto_hashblocks(h, m, 0, n);
|
||||
n = n % 128;
|
||||
for (i in 0...256) x[i] = 0;
|
||||
for (i in 0...n) x[i] = m[b - n + i];
|
||||
x[n] = 128;
|
||||
var sz = 256 - 128 * (n < 112 ? 1 : 0);
|
||||
x[sz - 9] = 0;
|
||||
ts64u(x, sz - 8, u64(Std.int(b / 0x20000000), b << 3));
|
||||
crypto_hashblocks(h, x, 0, sz);
|
||||
for (i in 0...64) out[i] = h[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function edAdd(p:Array<Array<Float>>, q:Array<Array<Float>>):Void {
|
||||
var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
|
||||
Z(a, p[1], p[0]); Z(t, q[1], q[0]); M(a, a, t);
|
||||
A(b, p[0], p[1]); A(t, q[0], q[1]); M(b, b, t);
|
||||
M(c, p[3], q[3]); M(c, c, D2);
|
||||
M(d, p[2], q[2]); A(d, d, d);
|
||||
Z(e, b, a); Z(f, d, c); A(g, d, c); A(h, b, a);
|
||||
M(p[0], e, f); M(p[1], h, g); M(p[2], g, f); M(p[3], e, h);
|
||||
}
|
||||
|
||||
static function cswap(p:Array<Array<Float>>, q:Array<Array<Float>>, b:Int):Void {
|
||||
for (i in 0...4) sel25519(p[i], q[i], b);
|
||||
}
|
||||
|
||||
static function edPack(r:Array<Int>, p:Array<Array<Float>>):Void {
|
||||
var tx = gf(), ty = gf(), zi = gf();
|
||||
inv25519(zi, p[2]); M(tx, p[0], zi); M(ty, p[1], zi);
|
||||
pack25519(r, ty);
|
||||
r[31] ^= par25519(tx) << 7;
|
||||
}
|
||||
|
||||
static function edScalarmult(p:Array<Array<Float>>, q:Array<Array<Float>>, s:Array<Int>):Void {
|
||||
set25519(p[0], gf0); set25519(p[1], gf1); set25519(p[2], gf1); set25519(p[3], gf0);
|
||||
var i = 255; while (i >= 0) {
|
||||
var b = (s[Std.int(i / 8)] >> (i & 7)) & 1;
|
||||
cswap(p, q, b);
|
||||
edAdd(q, p);
|
||||
edAdd(p, p);
|
||||
cswap(p, q, b);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
static function edScalarbase(p:Array<Array<Float>>, s:Array<Int>):Void {
|
||||
var q = [gf(), gf(), gf(), gf()];
|
||||
set25519(q[0], X); set25519(q[1], Y); set25519(q[2], gf1);
|
||||
M(q[3], X, Y);
|
||||
edScalarmult(p, q, s);
|
||||
}
|
||||
|
||||
static function crypto_sign_keypair(pk:Array<Int>, sk:Array<Int>, seeded:Bool, randomFn:Int->Array<Int>):Int {
|
||||
var d = [for (i in 0...64) 0];
|
||||
var p = [gf(), gf(), gf(), gf()];
|
||||
if (!seeded) {
|
||||
var rb = randomFn(32);
|
||||
for (i in 0...32) sk[i] = rb[i];
|
||||
}
|
||||
crypto_hash(d, sk, 32);
|
||||
d[0] &= 248; d[31] &= 127; d[31] |= 64;
|
||||
edScalarbase(p, d);
|
||||
edPack(pk, p);
|
||||
for (i in 0...32) sk[i + 32] = pk[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function modL(r:Array<Int>, roff:Int, x:Array<Float>):Void {
|
||||
var carry:Int, i:Int, j:Int, k:Int;
|
||||
i = 63; while (i >= 32) {
|
||||
carry = 0;
|
||||
j = i - 32; k = i - 12;
|
||||
while (j < k) {
|
||||
x[j] += carry - 16 * x[i] * L[j - (i - 32)];
|
||||
carry = Std.int(Math.floor((x[j] + 128) / 256));
|
||||
x[j] -= carry * 256;
|
||||
j++;
|
||||
}
|
||||
x[j] += carry;
|
||||
x[i] = 0;
|
||||
i--;
|
||||
}
|
||||
carry = 0;
|
||||
for (j in 0...32) {
|
||||
x[j] += carry - (Std.int(x[31]) >> 4) * L[j];
|
||||
carry = Std.int(x[j]) >> 8;
|
||||
x[j] = Std.int(x[j]) & 255;
|
||||
}
|
||||
for (j in 0...32) x[j] -= carry * L[j];
|
||||
for (i in 0...32) {
|
||||
x[i + 1] += Std.int(x[i]) >> 8;
|
||||
r[roff + i] = Std.int(x[i]) & 255;
|
||||
}
|
||||
}
|
||||
|
||||
static function reduce(r:Array<Int>):Void {
|
||||
var x = [for (i in 0...64) 0.0];
|
||||
for (i in 0...64) x[i] = r[i];
|
||||
for (i in 0...64) r[i] = 0;
|
||||
modL(r, 0, x);
|
||||
}
|
||||
|
||||
static function crypto_sign(sm:Array<Int>, m:Array<Int>, n:Int, sk:Array<Int>):Int {
|
||||
var d = [for (i in 0...64) 0], h = [for (i in 0...64) 0], r = [for (i in 0...64) 0];
|
||||
var x = [for (i in 0...64) 0.0];
|
||||
var p = [gf(), gf(), gf(), gf()];
|
||||
crypto_hash(d, sk, 32);
|
||||
d[0] &= 248; d[31] &= 127; d[31] |= 64;
|
||||
for (i in 0...n) sm[64 + i] = m[i];
|
||||
for (i in 0...32) sm[32 + i] = d[32 + i];
|
||||
var sm32 = [for (i in 0...n + 32) 0];
|
||||
for (i in 0...n + 32) sm32[i] = sm[32 + i];
|
||||
crypto_hash(r, sm32, n + 32);
|
||||
reduce(r);
|
||||
edScalarbase(p, r);
|
||||
edPack(sm, p);
|
||||
for (i in 32...64) sm[i] = sk[i];
|
||||
var smFull = [for (i in 0...n + 64) 0];
|
||||
for (i in 0...n + 64) smFull[i] = sm[i];
|
||||
crypto_hash(h, smFull, n + 64);
|
||||
reduce(h);
|
||||
for (i in 0...64) x[i] = 0;
|
||||
for (i in 0...32) x[i] = r[i];
|
||||
for (i in 0...32) for (j in 0...32) x[i + j] += h[i] * d[j];
|
||||
modL(sm, 32, x);
|
||||
return n + 64;
|
||||
}
|
||||
|
||||
static function unpackneg(r:Array<Array<Float>>, p:Array<Int>):Int {
|
||||
var t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
|
||||
set25519(r[2], gf1);
|
||||
unpack25519(r[1], p);
|
||||
S(num, r[1]); M(den, num, D); Z(num, num, r[2]); A(den, r[2], den);
|
||||
S(den2, den); S(den4, den2); M(den6, den4, den2);
|
||||
M(t, den6, num); M(t, t, den);
|
||||
pow2523(t, t);
|
||||
M(t, t, num); M(t, t, den); M(t, t, den);
|
||||
M(r[0], t, den);
|
||||
S(chk, r[0]); M(chk, chk, den);
|
||||
if (neq25519(chk, num) != 0) M(r[0], r[0], I);
|
||||
S(chk, r[0]); M(chk, chk, den);
|
||||
if (neq25519(chk, num) != 0) return -1;
|
||||
if (par25519(r[0]) == (p[31] >> 7)) Z(r[0], gf0, r[0]);
|
||||
M(r[3], r[0], r[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static function crypto_sign_open(m:Array<Int>, sm:Array<Int>, n:Int, pk:Array<Int>):Int {
|
||||
var t = [for (i in 0...32) 0], h = [for (i in 0...64) 0];
|
||||
var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()];
|
||||
if (n < 64) return -1;
|
||||
if (unpackneg(q, pk) != 0) return -1;
|
||||
for (i in 0...n) m[i] = sm[i];
|
||||
for (i in 0...32) m[i + 32] = pk[i];
|
||||
crypto_hash(h, m, n);
|
||||
reduce(h);
|
||||
edScalarmult(p, q, h);
|
||||
var sm32 = [for (i in 0...32) 0];
|
||||
for (i in 0...32) sm32[i] = sm[32 + i];
|
||||
edScalarbase(q, sm32);
|
||||
edAdd(p, q);
|
||||
edPack(t, p);
|
||||
n -= 64;
|
||||
if (crypto_verify_32(sm, 0, t, 0) != 0) {
|
||||
for (i in 0...n) m[i] = 0;
|
||||
return -1;
|
||||
}
|
||||
for (i in 0...n) m[i] = sm[i + 64];
|
||||
return n;
|
||||
}
|
||||
|
||||
public static inline var crypto_box_PUBLICKEYBYTES = 32;
|
||||
public static inline var crypto_box_SECRETKEYBYTES = 32;
|
||||
public static inline var crypto_box_NONCEBYTES = 24;
|
||||
public static inline var crypto_box_ZEROBYTES = 32;
|
||||
public static inline var crypto_box_BOXZEROBYTES = 16;
|
||||
public static inline var crypto_sign_BYTES = 64;
|
||||
public static inline var crypto_sign_PUBLICKEYBYTES = 32;
|
||||
public static inline var crypto_sign_SECRETKEYBYTES = 64;
|
||||
public static inline var crypto_sign_SEEDBYTES = 32;
|
||||
public static inline var crypto_hash_BYTES = 64;
|
||||
|
||||
public static function hash(msg:Bytes):Bytes {
|
||||
var m = bytesToArray(msg);
|
||||
var out = [for (i in 0...64) 0];
|
||||
crypto_hash(out, m, msg.length);
|
||||
return arrayToBytes(out);
|
||||
}
|
||||
|
||||
public static function signDetached(msg:Bytes, secretKey:Bytes):Bytes {
|
||||
var m = bytesToArray(msg);
|
||||
var sk = bytesToArray(secretKey);
|
||||
var sm = [for (i in 0...crypto_sign_BYTES + msg.length) 0];
|
||||
crypto_sign(sm, m, msg.length, sk);
|
||||
var sig = [for (i in 0...crypto_sign_BYTES) 0];
|
||||
for (i in 0...crypto_sign_BYTES) sig[i] = sm[i];
|
||||
return arrayToBytes(sig);
|
||||
}
|
||||
|
||||
public static function signDetachedVerify(msg:Bytes, sig:Bytes, publicKey:Bytes):Bool {
|
||||
var sm = [for (i in 0...crypto_sign_BYTES + msg.length) 0];
|
||||
var m = [for (i in 0...crypto_sign_BYTES + msg.length) 0];
|
||||
var sigArr = bytesToArray(sig);
|
||||
var msgArr = bytesToArray(msg);
|
||||
for (i in 0...crypto_sign_BYTES) sm[i] = sigArr[i];
|
||||
for (i in 0...msg.length) sm[i + crypto_sign_BYTES] = msgArr[i];
|
||||
var pk = bytesToArray(publicKey);
|
||||
return crypto_sign_open(m, sm, sm.length, pk) >= 0;
|
||||
}
|
||||
|
||||
public static function signKeyPairFromSeed(seed:Bytes):{publicKey:Bytes, secretKey:Bytes} {
|
||||
var pk = [for (i in 0...32) 0];
|
||||
var sk = [for (i in 0...64) 0];
|
||||
var seedArr = bytesToArray(seed);
|
||||
for (i in 0...32) sk[i] = seedArr[i];
|
||||
crypto_sign_keypair(pk, sk, true, null);
|
||||
return {publicKey: arrayToBytes(pk), secretKey: arrayToBytes(sk)};
|
||||
}
|
||||
|
||||
public static function boxKeyPair():{publicKey:Bytes, secretKey:Bytes} {
|
||||
var pk = [for (i in 0...32) 0];
|
||||
var sk = [for (i in 0...32) 0];
|
||||
crypto_box_keypair(pk, sk, randomBytesArray);
|
||||
return {publicKey: arrayToBytes(pk), secretKey: arrayToBytes(sk)};
|
||||
}
|
||||
|
||||
public static function box(msg:Bytes, nonce:Bytes, publicKey:Bytes, secretKey:Bytes):Bytes {
|
||||
var m = [for (i in 0...crypto_box_ZEROBYTES + msg.length) 0];
|
||||
var c = [for (i in 0...m.length) 0];
|
||||
var msgArr = bytesToArray(msg);
|
||||
for (i in 0...msg.length) m[i + crypto_box_ZEROBYTES] = msgArr[i];
|
||||
var n = bytesToArray(nonce);
|
||||
var pk = bytesToArray(publicKey);
|
||||
var sk = bytesToArray(secretKey);
|
||||
crypto_box(c, m, m.length, n, pk, sk);
|
||||
var result = [for (i in 0...c.length - crypto_box_BOXZEROBYTES) 0];
|
||||
for (i in 0...result.length) result[i] = c[i + crypto_box_BOXZEROBYTES];
|
||||
return arrayToBytes(result);
|
||||
}
|
||||
|
||||
public static function boxOpen(box:Bytes, nonce:Bytes, publicKey:Bytes, secretKey:Bytes):Bytes {
|
||||
var c = [for (i in 0...crypto_box_BOXZEROBYTES + box.length) 0];
|
||||
var m = [for (i in 0...c.length) 0];
|
||||
var boxArr = bytesToArray(box);
|
||||
for (i in 0...box.length) c[i + crypto_box_BOXZEROBYTES] = boxArr[i];
|
||||
if (c.length < 32) return null;
|
||||
var n = bytesToArray(nonce);
|
||||
var pk = bytesToArray(publicKey);
|
||||
var sk = bytesToArray(secretKey);
|
||||
if (crypto_box_open(m, c, c.length, n, pk, sk) != 0) return null;
|
||||
var result = [for (i in 0...m.length - crypto_box_ZEROBYTES) 0];
|
||||
for (i in 0...result.length) result[i] = m[i + crypto_box_ZEROBYTES];
|
||||
return arrayToBytes(result);
|
||||
}
|
||||
|
||||
public static var boxNonceLength(get, null):Int;
|
||||
static inline function get_boxNonceLength():Int { return crypto_box_NONCEBYTES; }
|
||||
|
||||
static function bytesToArray(b:Bytes):Array<Int> {
|
||||
var a = [for (i in 0...b.length) 0];
|
||||
for (i in 0...b.length) a[i] = b.get(i);
|
||||
return a;
|
||||
}
|
||||
|
||||
static function arrayToBytes(a:Array<Int>):Bytes {
|
||||
var b = Bytes.alloc(a.length);
|
||||
for (i in 0...a.length) b.set(i, a[i] & 0xff);
|
||||
return b;
|
||||
}
|
||||
|
||||
static function randomBytesArray(n:Int):Array<Int> {
|
||||
var b = Crypto.randomBytes(n);
|
||||
var a = [for (i in 0...n) 0];
|
||||
for (i in 0...n) a[i] = b.get(i);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
57
leenkx/Sources/leenkx/network/torrent/Types.hx
Normal file
57
leenkx/Sources/leenkx/network/torrent/Types.hx
Normal file
@ -0,0 +1,57 @@
|
||||
package leenkx.network.torrent;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
|
||||
typedef LeenkxOptions = {
|
||||
@:optional var seed:String;
|
||||
@:optional var keyPair:{publicKey:Bytes, secretKey:Bytes};
|
||||
@:optional var timeout:Int;
|
||||
@:optional var announce:Array<String>;
|
||||
@:optional var iceServers:Array<String>;
|
||||
@:optional var tracker:Dynamic;
|
||||
@:optional var wtOpts:Dynamic;
|
||||
@:optional var torrentOpts:Dynamic;
|
||||
@:optional var torrent:Dynamic;
|
||||
@:optional var wt:Dynamic;
|
||||
@:optional var heartbeat:Int;
|
||||
@:optional var deferJoin:Bool;
|
||||
@:optional var transport:String;
|
||||
}
|
||||
|
||||
typedef PeerInfo = {
|
||||
var pk:String;
|
||||
var ek:String;
|
||||
var last:Float;
|
||||
var index:Int;
|
||||
}
|
||||
|
||||
typedef Packet = {
|
||||
var t:Float;
|
||||
var i:String;
|
||||
var pk:String;
|
||||
var ek:String;
|
||||
var n:Bytes;
|
||||
@:optional var y:String;
|
||||
@:optional var v:String;
|
||||
}
|
||||
|
||||
enum abstract PacketType(String) from String to String {
|
||||
var Message = "m";
|
||||
var Ping = "p";
|
||||
var Leave = "x";
|
||||
}
|
||||
|
||||
enum abstract LeenkxEvent(String) from String to String {
|
||||
var Torrent = "torrent";
|
||||
var Message = "message";
|
||||
var Seen = "seen";
|
||||
var Left = "left";
|
||||
var Server = "server";
|
||||
var Ping = "ping";
|
||||
var Timeout = "timeout";
|
||||
var Connections = "connections";
|
||||
var WireSeen = "wireseen";
|
||||
var WireLeft = "wireleft";
|
||||
var Tracker = "tracker";
|
||||
var Announce = "announce";
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user