Skip to content

Commit 26e2628

Browse files
committed
Reworked saves to use prefab parentage instead of builder whitelist, this fixes saving newly introduced items. Moved 'use exact coordinates' option from Save to Load, so builder can choose to spawn exact or relative. Made a new connection for Save/Load to prevent interference
1 parent e7bb316 commit 26e2628

File tree

7 files changed

+227
-415
lines changed

7 files changed

+227
-415
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ https://github.com/edencomputing/attprefabulator/releases/
1111
<a href="https://github.com/edencomputing/attprefabulator/releases/"><img src="public/images/prefabulator_controls.png" width="30%" height="30%" alt="Prefab Controls"></a>
1212

1313
### Release Notes
14+
- v1.5.3 - Reworked saves to use prefab parentage instead of builder whitelist, this fixes saving newly introduced items. Moved "use exact coordinates" option from Save to Load, so builder can choose to spawn exact or relative. Made a new connection for Save/Load to prevent interference
15+
1416
- v1.5.2 - Added subscription selections to the console so you can control which events you watch
1517

1618
- v1.5.1 - Finally added the command console, which has been "coming soon" for far too long.

index.js

Lines changed: 160 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ function wsSendJSON( data )
5555
// Main connections (used by websocket handlers)
5656
var attSession;
5757
var attServer;
58+
// The websocket connections to server
5859
var attConsole;
5960
var attSubscriptions;
61+
var attSaveLoad;
6062

6163
const builderKeyPrefabString = "39744,117,39744,3292217970,1130291577,3265257749,1032887442,3211394956,1065353216,3184467828,1051372203,1454441398,32,3392848145,2290978823,418,3292217970,1130291577,3265257749,1032887442,3211394956,1053152122,3184467828,3221225472,0,0,0,0,0,0,0,0,"
6264
//Utility helper functions and prototypes
@@ -97,6 +99,24 @@ function setSubscrptionsConnection( serverName )
9799
}
98100
}
99101

102+
function setSaveLoadConnection( serverName )
103+
{
104+
var { Connection } = require('att-websockets')
105+
delete require.cache[require.resolve('att-websockets')]
106+
107+
attSaveLoad = new Connection( serverName )
108+
attSaveLoad.onError = ( e ) => {
109+
console.log( e )
110+
throw( e )
111+
}
112+
attSaveLoad.onMessage = ( message ) => {
113+
if ( !!wsSocket )
114+
{
115+
wsSendJSON( message )
116+
}
117+
}
118+
}
119+
100120
function setATTSession( )
101121
{
102122
var { Sessions, Servers } = require('alta-jsapi')
@@ -274,11 +294,14 @@ server.post('/servers', asyncMid( async(req, res, next) =>{
274294
var details = await attServer.joinConsole( serverId )
275295
if ( details.allowed )
276296
{
277-
console.log("Connected to server: "+ selectedServer.name )
297+
console.log( "Connecting to server: "+ selectedServer.name )
278298
setConnection( selectedServer.name )
279299
setSubscrptionsConnection( selectedServer.Name )
300+
setSaveLoadConnection( selectedServer.name )
280301
await attConsole.connect( details.connection.address, details.connection.websocket_port, details.token )
281302
await attSubscriptions.connect( details.connection.address, details.connection.websocket_port, details.token )
303+
await attSaveLoad.connect( details.connection.address, details.connection.websocket_port, details.token )
304+
console.log("Connected to server: "+ selectedServer.name )
282305
res.redirect('/control?serverName='+ selectedServer.name )
283306
return
284307
} else {
@@ -334,22 +357,23 @@ server.post('/save_prefabs', asyncMid( async( req, res, next ) => {
334357
{
335358
console.log( req.body )
336359
let response = {}
360+
let itemNames = {}
337361
let itemList = req.body.items
338-
let useExactCoordinates = ( req.body.exact == true ) ? true : false
362+
let useExactCoordinates = true
339363
let positionList = []
340364
attConsole.onMessage = ( message ) => {
341-
console.log( "command response message")
342-
console.log( message, message.data.Result, message.data.BufferResultString )
365+
//console.log( "save_prefabs command response message")
366+
//console.log( message, message.data.Result, message.data.BufferResultString )
343367
if ( message.data.Command.FullName == 'select.tostring')
344368
{
345369
let resultString = (message.data.ResultString.split('|',1))[0]
346370
let words = resultString.split(',')
347-
let prefab = spawnableItemsList.find( item => item.Hash == words[0] )
371+
let prefab = itemNames[words[0]]
348372

349373
let item = {
350374
'string' : resultString,
351375
'hash' : words[0],
352-
'Name' : ( !!prefab ) ? prefab.Name : '',
376+
'Name' : prefab,
353377
'Position' : new THREE.Vector3(
354378
unpackfloat( words[3] ), // x
355379
unpackfloat( words[4] ), // y
@@ -388,7 +412,7 @@ server.post('/save_prefabs', asyncMid( async( req, res, next ) => {
388412
y : rad2dec( item.roteuler.y ),
389413
z : rad2dec( item.roteuler.z ),
390414
}
391-
console.log( "item tostring:", item )
415+
//console.log( "item tostring:", item )
392416
positionList.push(item)
393417
}
394418
}
@@ -398,6 +422,8 @@ server.post('/save_prefabs', asyncMid( async( req, res, next ) => {
398422
for ( let i = 0; i < itemList.length; i++ )
399423
{
400424
let item = itemList[i]
425+
itemNames[ item.hash ] = item.name
426+
401427
let cmd = "select "+ item.id
402428
await attConsole.send(cmd)
403429
await attConsole.send("select tostring")
@@ -412,38 +438,9 @@ server.post('/save_prefabs', asyncMid( async( req, res, next ) => {
412438
console.log( "final positions: ")
413439
console.log( positionList )
414440

415-
if ( !useExactCoordinates )
416-
{
417-
// Find the smallest values for x,y,z
418-
min_x = positionList[0].Position.x;
419-
min_y = positionList[0].Position.y;
420-
min_z = positionList[0].Position.z;
421-
for ( let i = 0; i < positionList.length; i++ )
422-
{
423-
let item = positionList[i]
424-
if ( item.Position.x < min_x ) { min_x = item.Position.x }
425-
if ( item.Position.y < min_y ) { min_y = item.Position.y }
426-
if ( item.Position.z < min_z ) { min_z = item.Position.z }
427-
}
428-
429-
// remove the minimum offsets to bring the item to root
430-
console.log( "min_x: "+ min_x )
431-
console.log( "min_y: "+ min_y )
432-
console.log( "min_z: "+ min_z )
433-
for ( let i = 0; i < positionList.length; i++ )
434-
{
435-
let item = positionList[i]
436-
item.Position.x = item.Position.x - min_x
437-
item.Position.y = item.Position.y - min_y
438-
item.Position.z = item.Position.z - min_z
439-
positionList[i] = item;
440-
}
441-
console.log("offset positions:")
442-
console.log( positionList )
443-
}
444441

445442
// Write it out to the file
446-
let ts = moment().valueOf()
443+
let ts = moment().format("YYYYMMDD_kkmmss_SSSS")
447444
let fprefix = req.sessionID + "_"
448445
let filename = ts + ".json"
449446
let jsonDataset = {
@@ -524,8 +521,9 @@ server.post('/load_prefabs_input', asyncMid( async( req, res, next ) => {
524521
server.post('/load_prefabs', asyncMid( async( req, res, next ) => {
525522
if ( authenticated( req ) )
526523
{
524+
console.log( req.body )
527525
let prefabList = loadedPrefabLists[ req.body.md5sum ]
528-
let useExactCoordinates = ( prefabList.header.exact )
526+
let useExactCoordinates = parseBool( req.body.useExactCoords )
529527
let userId = attSession.getUserId()
530528
let moffset = [
531529
parseFloat(req.body.moffset_x),
@@ -556,10 +554,41 @@ server.post('/load_prefabs', asyncMid( async( req, res, next ) => {
556554
let conn = attConsole
557555

558556
// Apply the offset to the supplied coordinates
559-
console.log("original prefabs list")
560-
console.log( prefabList.prefabs)
561-
console.log("slice copy")
562-
console.log( translatedPrefabs )
557+
//console.log("original prefabs list")
558+
//console.log( prefabList.prefabs)
559+
//console.log("slice copy")
560+
//console.log( translatedPrefabs )
561+
console.log( "useExactCoordinates: ", useExactCoordinates )
562+
if ( !useExactCoordinates )
563+
{
564+
// Find the smallest values for x,y,z
565+
min_x = translatedPrefabs[0].Position.x;
566+
min_y = translatedPrefabs[0].Position.y;
567+
min_z = translatedPrefabs[0].Position.z;
568+
for ( let i = 0; i < translatedPrefabs.length; i++ )
569+
{
570+
let item = translatedPrefabs[i]
571+
if ( item.Position.x < min_x ) { min_x = item.Position.x }
572+
if ( item.Position.y < min_y ) { min_y = item.Position.y }
573+
if ( item.Position.z < min_z ) { min_z = item.Position.z }
574+
}
575+
576+
// remove the minimum offsets to bring the item to root
577+
console.log( "min_x: "+ min_x )
578+
console.log( "min_y: "+ min_y )
579+
console.log( "min_z: "+ min_z )
580+
for ( let i = 0; i < translatedPrefabs.length; i++ )
581+
{
582+
let item = translatedPrefabs[i]
583+
item.Position.x = item.Position.x - min_x
584+
item.Position.y = item.Position.y - min_y
585+
item.Position.z = item.Position.z - min_z
586+
translatedPrefabs[i] = item;
587+
}
588+
console.log("offset positions:")
589+
console.log( translatedPrefabs )
590+
}
591+
563592
for( let i = 0; i < translatedPrefabs.length; i++ )
564593
{
565594
let item = translatedPrefabs[i]
@@ -569,11 +598,10 @@ server.post('/load_prefabs', asyncMid( async( req, res, next ) => {
569598

570599
translatedPrefabs[i] = item
571600
}
572-
console.log("original")
573-
console.log( prefabList.prefabs )
574-
console.log("slice copy with predefined offsets")
575-
console.log( translatedPrefabs )
576-
601+
//console.log("original")
602+
//console.log( prefabList.prefabs )
603+
//console.log("slice copy with predefined offsets")
604+
//console.log( translatedPrefabs )
577605

578606
if ( useExactCoordinates )
579607
{
@@ -629,15 +657,15 @@ server.post('/load_prefabs', asyncMid( async( req, res, next ) => {
629657
if ( ind === undefined ) ind = 0
630658
if ( translatedPrefabs.length > 0 && ind < translatedPrefabs.length )
631659
{
632-
console.log("spawning prefab from list ["+ ind +"]")
660+
//console.log("spawning prefab from list ["+ ind +"]")
633661
let titem = translatedPrefabs[ind]
634-
console.log( titem )
662+
//console.log( titem )
635663
let pos = titem.Position
636664
let rot = titem.Rotation
637665

638666
conn.onMessage = ( message ) => {
639-
console.log( "spawnPrefabsFromList message:")
640-
console.log( message )
667+
//console.log( "spawnPrefabsFromList message:")
668+
//console.log( message )
641669
if ( !!message.data.Command )
642670
{
643671
let command = ''
@@ -735,7 +763,7 @@ wsAddHandler( 'unsubscribe', async ( data ) => {
735763
}
736764
})
737765

738-
// attConsole websocket controls
766+
// Subscription socket controls
739767
var wsMap = {}
740768
wsAddHandler( 'send_command', async ( data ) => {
741769
attSubscriptions.onMessage = ( message ) => {
@@ -750,6 +778,7 @@ wsAddHandler( 'send_command', async ( data ) => {
750778
await attSubscriptions.send( data.command )
751779
})
752780

781+
// attConsole websocket controls
753782
wsAddHandler( 'set_angle', ( data ) => {
754783
wsMap.rotateAngle = data.angle
755784
console.log( "New rotation angle: "+ wsMap.rotateAngle )
@@ -807,6 +836,80 @@ wsAddHandler('snap-ground', async( data ) => {
807836
wsSendJSON({ result: 'OK', data: data })
808837
})
809838

839+
// SaveLoad websocket controls
840+
wsAddHandler('select_find_save', async( data ) => {
841+
let scanDiameter = 10
842+
if ( !!data.diameter ) { scanDiameter = data.diameter }
843+
let userId = attSession.getUserId()
844+
let prefabCount = 0
845+
let prefabsScanned = 0
846+
data.scanitems = []
847+
848+
// These items should never be scanned
849+
let blacklistItems = {
850+
'0' : 'Respawn Point',
851+
'37940' : 'Discovery Landmark',
852+
'49582': 'VR Player Character New'
853+
}
854+
855+
attSaveLoad.onMessage = ( message ) => {
856+
// Message handlers for the save system
857+
//console.log( "select_find_save.onmessage: ", message )
858+
859+
if ( !!message.data.Exception )
860+
{
861+
data.message = message
862+
wsSendJSON({ result: 'Fail', data: data})
863+
}
864+
865+
if ( !!message.data.Command.FullName )
866+
{
867+
if ( !message.data.Result )
868+
{
869+
data.message = message
870+
wsSendJSON({ result:"Fail", data: data })
871+
} else {
872+
switch( message.data.Command.FullName )
873+
{
874+
case "select.find":
875+
// For each result item, run select get <id>
876+
console.log( "Found prefabs: ", message.data.Result )
877+
prefabCount = message.data.Result.length;
878+
for ( let i = 0; i < prefabCount; i++ )
879+
{
880+
attSaveLoad.send(`select get ${message.data.Result[i].Identifier}`)
881+
}
882+
break;
883+
884+
case "select.get":
885+
// For each item, add to save array if ChunkingParent == 0
886+
prefabsScanned++
887+
if ( message.data.Result.ChunkingParent == 0 )
888+
{
889+
if ( !blacklistItems[ message.data.Result.PrefabHash ])
890+
{
891+
console.log( "select.get saveable item: ", message.data.Result )
892+
data.scanitems.push( message.data.Result )
893+
}
894+
}
895+
if ( prefabsScanned == prefabCount )
896+
{
897+
console.log( "sending result: ", data )
898+
wsSendJSON({ result: 'OK', data: data })
899+
}
900+
break;
901+
}
902+
}
903+
}
904+
}
905+
906+
console.log( "select find save")
907+
await attSaveLoad.send( `select find ${userId} ${scanDiameter}` )
908+
909+
})
910+
911+
912+
810913
// TODO: replace these with websocket handlers
811914
server.post('/ajax', asyncMid( async( req, res, next ) => {
812915
console.log( req.body )
@@ -1256,4 +1359,9 @@ function unpackfloat( value )
12561359
function rad2dec( angle )
12571360
{
12581361
return angle * ( 180 / Math.PI )
1362+
}
1363+
1364+
function parseBool(val)
1365+
{
1366+
return val === true || val === "true"
12591367
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prefabulator",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"productName": "Prefabulator",
55
"description": "Add, remove and modify prefabs in A Township Tale",
66
"main": "electron-start.js",

0 commit comments

Comments
 (0)