Difference between revisions of "Snippets"

From SolarStrike wiki
Jump to: navigation, search
(Random hash generator)
(Experience per Hour)
 
(9 intermediate revisions by 5 users not shown)
Line 8: Line 8:
  
 
'''random_hash()'''
 
'''random_hash()'''
 +
  
 
'''Description: '''
 
'''Description: '''
Line 17: Line 18:
 
'''License: ''' Public Domain
 
'''License: ''' Public Domain
  
''Author: ''' "Elverion"
+
'''Author: ''' "Elverion"
  
 
<source lang="lua">
 
<source lang="lua">
Line 28: Line 29:
  
 
   -- create an array of all acceptable characters that can be put in our hash.
 
   -- create an array of all acceptable characters that can be put in our hash.
   hash_chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E",
+
   local hash_chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E",
 
                 "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
 
                 "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
 
                 "U", "V", "W", "X", "Y", "Z"};
 
                 "U", "V", "W", "X", "Y", "Z"};
Line 38: Line 39:
  
 
   return holder;
 
   return holder;
 +
end
 +
</source>
 +
 +
== memory Write String ==
 +
'''memoryWriteString(proc, address, msg)'''
 +
 +
 +
'''Description: '''
 +
Enters a string into memory.
 +
 +
'''Purpose: '''
 +
So strings can be written. Be sure not to write a string that's greater than the strings allocated memory.
 +
 +
'''License: ''' Public Domain
 +
 +
'''Author: ''' "zerosignal"
 +
 +
<source lang="lua">
 +
function memoryWriteString(proc, address, msg)
 +
  local l_msg_len = msg:len()
 +
  local l_char = nil;
 +
  for i=0, (l_msg_len-1) do
 +
    l_char = msg:byte(i)
 +
    --debug_message("char: " .. l_char);
 +
    memoryWriteByte(proc, address + i, l_char)
 +
  end
 +
  memoryWriteByte(proc, address + l_msg_len, 0)
 +
end
 +
</source>
 +
 +
== Experience per Hour ==
 +
 +
 +
'''Description: '''
 +
Estimates the total experience in a hour. Can be extended for Gold, Kills or anything else.
 +
 +
'''License: ''' Public Domain
 +
 +
'''Author: ''' "Elverion"
 +
 +
<source lang="lua">
 +
local expGained = 0;
 +
local startTime = os.time();
 +
local lastAnnounce = os.time();
 +
 +
local lastTotalExp = 0;
 +
while(true) do
 +
  local newTotalExp = memoryReadInt(proc, experienceaddr, experience_offset);
 +
  if( newTotalExp > lastTotalExp ) then
 +
    expGained = expGained + (newTotalExp - lastTotalExp);
 +
    lastTotalExp = newTotalExp;
 +
  end
 +
 +
  -- has 10 seconds elapsed? update the exp/min
 +
  if( os.difftime(os.time(), lastAnnounce) > 10 ) then
 +
    local timeElapsed = os.difftime(os.time(), startTime);
 +
    local expPerMin = 0;
 +
    if( timeElapsed == 0 ) then timeElapsed = 1; end; -- prevent division by zero
 +
 +
    expPerMin = expGained/(timeElapsed/60.0);
 +
    lastAnnounce = os.time();
 +
    printf("Exp/min: %0.1f\n" expPerMin);
 +
  end
 +
 
end
 
end
 
</source>
 
</source>

Latest revision as of 15:06, 2 January 2012

Note:

Code posted here is considered Public Domain unless otherwise noted. Be aware of this before posting your code! You may release your code here only under the following licenses: Public Domain, BSD, GPL, or LGPL.


Random hash generator

random_hash(len)

random_hash()


Description: Generates a random filled hash containing numbers and (capitol) letters. It will generate and return a hash that is 'len' characters long. If 'len' is not specified, then a 32 character hash will be assumed.

Purpose: Random hashes are, often, used in place of passwords. It is considered more secure than storing a reversible hash simply because it cannot be reversed (as it contains no real information). For example, after a successful login, a server might generate a random hash for that specific user. The client (user) will now use the random hash for future authentication instead of the password.

License: Public Domain

Author: "Elverion"

-- Generates a random hash with the specified length.
-- If length is less than or equal to zero, or unspecified, 32 bytes will be assumed.
function random_hash(len)
  if( len == nil or len <= 0 ) then len = 32; end;
  local holder = ""; -- a string for holding our hash temporarily


  -- create an array of all acceptable characters that can be put in our hash.
  local hash_chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E",
                "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                "U", "V", "W", "X", "Y", "Z"};

  for i = 1, len do
    local index = math.random(1, #hash_chars);
    holder = holder .. hash_chars[index];
  end

  return holder;
end

memory Write String

memoryWriteString(proc, address, msg)


Description: Enters a string into memory.

Purpose: So strings can be written. Be sure not to write a string that's greater than the strings allocated memory.

License: Public Domain

Author: "zerosignal"

function memoryWriteString(proc, address, msg)
  local l_msg_len = msg:len()
  local l_char = nil;
  for i=0, (l_msg_len-1) do
    l_char = msg:byte(i)
    --debug_message("char: " .. l_char);
    memoryWriteByte(proc, address + i, l_char)
  end
  memoryWriteByte(proc, address + l_msg_len, 0)
end

Experience per Hour

Description: Estimates the total experience in a hour. Can be extended for Gold, Kills or anything else.

License: Public Domain

Author: "Elverion"

local expGained = 0;
local startTime = os.time();
local lastAnnounce = os.time();

local lastTotalExp = 0;
while(true) do
  local newTotalExp = memoryReadInt(proc, experienceaddr, experience_offset);
  if( newTotalExp > lastTotalExp ) then
    expGained = expGained + (newTotalExp - lastTotalExp);
    lastTotalExp = newTotalExp;
  end

  -- has 10 seconds elapsed? update the exp/min
  if( os.difftime(os.time(), lastAnnounce) > 10 ) then
    local timeElapsed = os.difftime(os.time(), startTime);
    local expPerMin = 0;
    if( timeElapsed == 0 ) then timeElapsed = 1; end; -- prevent division by zero

    expPerMin = expGained/(timeElapsed/60.0);
    lastAnnounce = os.time();
    printf("Exp/min: %0.1f\n" expPerMin);
  end

end