Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

part:uid changes every time I load a craft #782

Closed
abenkovskii opened this issue Apr 12, 2015 · 6 comments
Closed

part:uid changes every time I load a craft #782

abenkovskii opened this issue Apr 12, 2015 · 6 comments

Comments

@abenkovskii
Copy link
Contributor

What the title says. According to this page KSP API exposes a part id that never changes and is stored in the save file. It would be nice to have part:uid that never changes.

@erendrake
Copy link
Member

The old part UID was removed from the api a few KSP releases ago. When that happened I reimplemented it with a generic hashcode which changes every time because I thought it would be enough to be unique in a mission.

You could use the part:tag value. As it does not change.

@abenkovskii
Copy link
Contributor Author

Because I'll have to set part:tag using a script and there is no easy way of making part:tag unique i ksp saved game. Also: I sort of want to use part:tag for other purposes than just storing a unique identifier.

@erendrake
Copy link
Member

You don't have to set it with a script. You can set it with the right click menu in the VAB.

@TDW89
Copy link
Contributor

TDW89 commented Apr 12, 2015

@erendrake This has mostly come up because I was writing a script (with assistance from @abenkovskii ) that tried to link volumes to their respective parts (without going through and doing it all by hand). The script in its current state runs fine until you reload the vessel its running on, in which case it goes into a boot loop.
boot.ks:

declare found.
declare fileList.
until found {
 list files in fileList.
 for file in fileList {
  if file = "exec.ks" {
   set found to true.
  }.
 }.
}.
log "idle:add("+mac+")." to idle.ks.

exec.ks:

declare parameter code.
log "" to exec.tmp.
delete exec.tmp.
log code to exec.tmp.
run exec.tmp.

boot_control.ks:

wait 0.001. // for physics to load
print "start".
declare controlTag.
set controlTag to "control".
copy exec from 0.
print "copying exec".
declare volList.
declare fileList.
list volumes in volList.
declare vol.
set vol to volList:iterator.
declare cpuList.
set cpuList to ship:modulesnamed("kOSProcessor").
declare cpu.
declare my_mac.
set my_mac to false.
declare mac.
declare found.
declare warning.

print 0.

Switch to 1.
set found to false.
list files in fileList.
for file in filelist {
 if file = "my_uid.ks" {
  set found to true.
 }.
}.
print 1.
if found {
 run my_uid.ks.
 set my_mac to mac.
 set found to false.
 for cpu in cpuList {
  if cpu:part:tag = controlTag and cpu:part:uid = my_mac {
   set found to true.
   break.
  }.
 }.
 if not found {
  print "This isnt your boot script.".
  reboot.
 }.
}
else {
 until my_mac <> false {
  for cpu in cpuList { //Find yourself.
   if cpu:part:tag = controlTag {
    set my_mac to cpu:part:uid.
    log "set mac to "+my_mac+"." to my_uid.ks.
    rename volume 1 to "MAC_"+my_mac.
   }.
  }.
  if my_mac = 0 and not warning {
   set warning to true.
   print "Please tag the part this boot script".
   print "is running on as  ''control''.".
  }.
 }.
 if warning {
  print " ".
  print "Thanks".
 }.
}.
print 2.
vol:reset.
vol:next.  //to get onto list.
vol:next.  //avoid the archive.
vol:next.  //avoid volume 1.
until vol:atend {  // store a copy of other volumes boot files on control
 switch to vol:index.
 set found to false.
 list files in fileList.
 for file in filelist {
  if file = "my_uid.ks" {
   set found to true.
  }.
 }.
 if not found {
  log "" to boot.ks.
  rename boot.ks to "boot_"+vol:index+".ks".
  copy "boot_"+vol:index+".ks" to "MAC_"+my_mac.
  delete "boot_"+vol:index+".ks".
 }.
 vol:next.
}.
print 3.
for cpu in cpuList { // leave an identifier file in each volume.
 set mac to cpu:part:uid.
 if mac <> my_mac {
  vol:reset.
  vol:next. // to get onto list.
  vol:next. //no point doing this with the archive.
  vol:next. //volume 1 already identified.
  until vol:atend {
   set found to false.
   switch to vol:index.
   list files in fileList.
   for file in filelist {
    if file = "my_uid.tmp" or file = "my_uid.ks" {
     set found to true.
     break.
    }.
   }.
   if not found {
    log "" to idflag.ks.
    delete idflag.ks.
    log "set mac to "+mac+"." to idflag.tmp.
    log "" to boot.ks.
    delete boot.ks.
    log "rename idflag.tmp to my_uid.tmp." to boot.ks.
    log "delete boot.ks." to boot.ks.
   }.
  vol:next.
  }.
  cpu:doevent("toggle power").
  wait 0.001.
  cpu:doevent("toggle power").
  wait 0.05.
 }.
}.
print 4.
vol:reset.
vol:next. //to get onto list.
vol:next. //avoid the archive.
vol:next. //avoid yourself.
until vol:atend { //links
 switch to vol:index.
 set found to false.
 list files in fileList.
 for file in filelist {
  if file = "my_uid.ks" {
   set found to true.
  }.
 }.
 If not found {
  switch to "MAC_"+my_mac.
  copy my_uid.tmp from vol:index.
  rename my_uid.tmp to "uid_"+vol:index+".ks".
  run exec("run uid_"+vol:index+".ks.").
  delete "uid_"+vol:index+".ks".
  switch to vol:index.
  rename my_uid.tmp to my_uid.ks.
  rename volume vol:index to "MAC_"+mac.
  log "switch to 1." to boot.ks. //just to be sure it running its own ID
  log "run my_uid.ks." to boot.ks.
  log "set my_mac to mac." to boot.ks.
  log "switch to MAC_"+my_mac+"." to boot.ks.
  log "run boot_"+mac+".ks." to boot.ks.
  switch to "MAC_"+my_mac.
  rename "boot_"+vol:index+".ks" to "boot_"+mac+".ks".
  for cpu in cpuList {
   if cpu:part:uid = mac {
    cpu:doevent("toggle power").
    wait 0.01.
    cpu:doevent("toggle power").
    wait 0.01.
   }.
  }.
 }.
 vol:next.
}.
print 5.
for cpu in cpuList {
 if cpu:part:uid = my_mac {
  cpu:doaction("open terminal",true).
 }.
}.
print 6.
set idle to list().
set idleSorted to list().
copy "sort_find.ks" from 0.
//set logcount to 0.                              //uncomment for debugging
until ag10 {
 list files in fileList.
 for file in fileList {
  if file = "idle.ks" {
   run exec("run idle.ks.").
   //rename idle.ks to "idlelog"+logcount+".log". //uncomment for debugging
   //set logcount to logcount+1.                  //uncomment for debugging
   delete idle.ks.                                //comment for debugging
   break.
  }.
 }.
 if idle:length > 0 {
  for cpu in idle {
   if idleSorted:empty {
    set listmin to 1.
   }
   else {
    run sort_find(cpu,idleSorted).
   }.
   if listmin > idleSorted:length -1 {
    idleSorted:add(cpu).
   }
   else {
    idleSorted:insert(listmin,cpu).
   }.
  }.
  idle:clear.
 }
 else {
//Asign tasks to cores on the idle list unsing run on. and remove them from it.
//I have some list sorting algarithums that i need to get working properly which should speed this up.
//cores need to be told to reboot after compleating their task to rejoin the idle list.

 }.
}.
print 7.

By setting the tag of 1 core to control and giving it the boot script boot_control.ks it would go through and tie a part uid to each core and leave a marker file in their volume.
While I could tag each part manually (tag appears to be get-only so cant be set by a script), it would require that I think of a unique tag for each one.

Edit:
the script got long because it had to had to be able to identify when one of the other cores 'stole' its boot script or so that it survived rebooting.

@abenkovskii
Copy link
Contributor Author

@TDW89 In the dev version part:tag is settable.

@TDW89
Copy link
Contributor

TDW89 commented Apr 12, 2015

@abenkovskii ok your rover idea should be able to handle everything then since it can just clock up a variable in a log on it's volume and assign that as a tag for every core as it launches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants