Thursday, 20 November 2008
 
  Home arrow Forum  
Main Menu
Home
News
Forum
Documentation
Download
Links
Administrator
Login Form
Welcome, Guest. Please login or register.
November 20, 2008, 08:01:28 PM
Username: Password:
Login with username, password and session length

Forgot your password?
DSLUA editor
DSLua Community
Welcome, Guest. Please login or register.
November 20, 2008, 08:01:28 PM
1723 Posts in 315 Topics by 5686 Members
Latest Member: BOAgifiarlJag
DSLua Community  |  DSLua - Best scripting language for Nintendo DS  |  Home-brew/Hacks/Games/Projects  |  DSLUA editor « previous next »
Pages: [1]
Author Topic: DSLUA editor  (Read 709 times)
abhuul
New Moon
*
Posts: 4


« on: June 11, 2008, 10:50:00 AM »

I have recently finished an editing program for Lua files, written in Lua so I don't have to use a text editor to make my scripts anymore. I have also integrated it with the MAIN script included with DSLua, so all you have to do is press Y to edit the selected file. I have had some issues with the program randomly crashing, though. It only seems to happen when I edit a program, quit back to MAIN, and then edit another program. Do I have to do something at the end of the script to clear variables or anything so they don't take up memory? Also, I'm not really sure how to attach the files, I tried before and it said they couldn't be saved or something like that.
Logged
Jeremysr
Waxing Gibbous
****
Posts: 254



WWW
« Reply #1 on: June 11, 2008, 01:36:43 PM »

Quote
Do I have to do something at the end of the script to clear variables or anything so they don't take up memory?

I'm not sure if that would cause it to crash but to free global variables from memory you set them to nil.

x = nil

Quote
Also, I'm not really sure how to attach the files, I tried before and it said they couldn't be saved or something like that.

What do you mean by "attach" the files?
Logged
abhuul
New Moon
*
Posts: 4


« Reply #2 on: June 11, 2008, 09:06:49 PM »

Quote
I'm not sure if that would cause it to crash but to free global variables from memory you set them to nil.

x = nil

I tried setting all my variables to nil at the end of my program, but it still crashed when I edited a script the second time.
An interesting thing I found, though, is that when the screen goes black, it won't reset with Start, like normal scripts, it only resets with Select.

Here's the Source:

My Include file, it contains a function to set up the screens and a function to return a character from a table:
-- set up screens for text output
function initScrTxt()
  local SCR_BOT  = 0
  BGBT = Screen.LoadTextBG()
  Screen.Initialize(SCR_BOT, BGBT)

  local SCR_TOP = 1
  BGTT = Screen.LoadTextBG()
  Screen.Initialize(SCR_TOP, BGTT)
end

function getChar()
  local chars = { {'.','Enter',' ','<','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',')','(','+','-','*','/','1','2','3','4','5','6','7','8','9','0',' ',' ' },{'.','Enter',' ','<','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','}','{','=',',','?','"','!','@','#','$','%','^','&','_',':',';','~','`' } }
  while true do
    BGBT:PrintXY(0,4,tostring(shift))
    BGBT:PrintXY(0,5,tostring(caps))
    if switch == true then
      BGBT:PrintXY(15,9,'^')
      BGBT:PrintXY(17,11,'>    ')
      BGBT:PrintXY(15,13,'v')
      BGBT:PrintXY(13,11,'<')
      if Pads.A() then
        while Pads.A() do end
        return 'save'
      elseif Pads.Up() then
        while Pads.Up() do end
        return 'up'
      elseif Pads.Right() then
        while Pads.Right() do end
        return 'right'
      elseif Pads.Down() then
        while Pads.Down() do end
        return 'down'
      elseif Pads.Left() then
        while Pads.Left() do end
        return 'left'
      elseif Pads.Select() then
        while Pads.Select() do end
        return 'exit'
      elseif Pads.Start() then
        while Pads.Start() do end
        switch = false
      elseif Pads.L() then
        while Pads.L() do end
        return 'first'
      elseif Pads.R() then
        while Pads.R() do end
        return 'last'
      end
    else
      if Pads.Start() then
        while Pads.Start() do end
        switch = true
      elseif Pads.Select() then
        while Pads.Select() do end
        temp = caps
        caps = shift
        shift = math.abs(shift - 1)
        shift = shift - temp
      elseif Pads.Up() then
        offset = 5
        if Pads.Left() then
          offset = 21
        elseif Pads.Right() then
          offset = 25
        end
      elseif Pads.Right() then
        offset = 9
        if Pads.Down() then
          offset = 29
        end
      elseif Pads.Down() then
        offset = 13
        if Pads.Left() then
          offset = 33
        end
      elseif Pads.Left() then
        offset = 17
      elseif Pads.L() then
        offset = 37
        if Pads.R() then
          offset = 45
        end
      elseif Pads.R() then
        offset = 41
      else
        offset = 1
      end
      up = chars[shift+caps+1][offset]
      right = chars[shift+caps+1][offset+1]
      down = chars[shift+caps+1][offset+2]
      left = chars[shift+caps+1][offset+3]
      BGBT:PrintXY(15,9,up)
      BGBT:PrintXY(17,11,right .. '    ')
      BGBT:PrintXY(15,13,down)
      BGBT:PrintXY(13,11,left)
      if Pads.X() or Pads.A() or Pads.B() or Pads.Y() then shift = shift - shift end
      if Pads.X() then
        while Pads.X() do end
        return up
      elseif Pads.A() then
        while Pads.A() do end
        return right
      elseif Pads.B() then
        while Pads.B() do end
        return down
      elseif Pads.Y() then
        while Pads.Y() do end
        return left
      end
    end
  end
end


Here's my actual editing program:

dofile("/SCRIPTS/editlua/include.lua")

function dispFile()
  BGTT:Clear()
  local rowtemp = row
  for i in pairs(filetab) do
    if i == curline then
      linescr = rowtemp
      local linetemp = string.sub(filetab,1,curlet-1) .. '|' .. string.sub(filetab,curlet,-1)
      BGTT:SetColor(2)
      BGTT:PrintXY(0,rowtemp,linetemp)
      BGTT:SetColor(0)
    else
      BGTT:PrintXY(0,rowtemp,filetab)
    end
    if(string.len(filetab) > 31) then
      rowtemp = rowtemp + 2
    else
      rowtemp = rowtemp + 1
    end
  end
end

initScrTxt()

BGBT:PrintXY(0,0,"Enter file path")

BGTT:PrintXY(0,0,filepath)
shift, caps = 0, 0
curline, curlet = 1, 1
linescr = 0
row = 0
switch = false
while true do
  letter=getChar()
  if letter=="Enter" then
    break
  elseif letter == '<' then
    filepath = string.sub(filepath,1,-2)
    BGTT:PrintXY(0,0,filepath .. ' ')
  else
    filepath = filepath .. letter
    BGTT:PrintXY(0,0,filepath)
  end
end
BGTT:Clear()

file = io.open(filepath,'rt')
l = 1
filetab = {}
for line in io.lines(filepath) do
  filetab[l] = line
  l = l + 1
end

dispFile()

while true do
  letter = getChar()
  if letter == 'Enter' then
    table.insert(filetab,curline + 1,'')
    parta = string.sub(filetab[curline],1,curlet-1)
    partb = string.sub(filetab[curline],curlet,-1)
    filetab[curline] = parta
    curline = curline + 1
    filetab[curline] = partb
    curlet = 1
  elseif letter == 'save' then
    fout = io.open(filepath,'wt')
    for i,v in ipairs(filetab) do
      fout:write(v .. '\n')
    end
    fout:close()
    BGBT:PrintXY(5,5,'Saved')
  elseif letter=="exit" then
    break
  elseif letter == '<' then
    if filetab[curline] == '' then
      curtemp = curline - 1
      lettemp = string.len(filetab[curtemp]) + 1
      table.remove(filetab,curline)
      curline = curline - 1
      curlet = lettemp
    elseif curlet == 1 then
      temp = filetab[curline]
      table.remove(filetab,curline)
      curline = curline - 1
      filetab[curline] = filetab[curline] .. temp
      curlet = lettemp
    else
      filetab[curline] =  string.sub(filetab[curline],1,curlet-2) .. string.sub(filetab[curline],curlet,-1)
      curlet = curlet - 1
    end
  elseif letter == 'left' then
    if curlet == 1 and curline > 1 then
      curline = curline - 1
      curlet = string.len(filetab[curline]) + 1
    else
      if curlet > 1 then
        curlet = curlet - 1
      end
    end
  elseif letter == 'right' then
    if curlet == string.len(filetab[curline]) + 1 and curline < table.getn(filetab) then
      curline = curline + 1
      curlet = 1
    elseif curlet < string.len(filetab[curline])+1  then
      curlet = curlet + 1
    end
  elseif letter == 'up' then
    if linescr < 7 and row < 0 then
      if string.len(filetab[curline-1]) <= 31 then
        row = row + 1
      elseif string.len(filetab[curline-1]) > 31 then
        row = row + 2
      end
    end
    if curline > 1 then curline = curline - 1 end
    if filetab[curline] == '' then curlet = 1 end
    if curlet >string.len(filetab[curline])+1 then
      curlet = string.len(filetab[curline]) + 1
    end
  elseif letter == 'down' then
    if curline < table.getn(filetab) then
      if linescr > 16 then
        if string.len(filetab[curline]) <= 31 then
          row = row - 1
        elseif string.len(filetab[curline]) >31 then
          row = row - 2
        end
      end
      curline = curline + 1
    end
    if filetab[curline] == '' then curlet = 1 end
    if curlet >string.len(filetab[curline])+1 then
      curlet = string.len(filetab[curline]) + 1
    end
  elseif letter == 'first' then
    curlet = 1
  elseif letter == 'last' then
    curlet = string.len(filetab[curline]) + 1
  else
    filetab[curline] = string.sub(filetab[curline],1,curlet-1) .. letter .. string.sub(filetab[curline],curlet,-1)
    curlet = curlet + 1
  end
  dispFile()
  Screen.WaitForVBL()
end

i,v,l = nil, nil, nil
filetab, curline, curlet, row = nil, nil, nil, nil
linescr = nil
shift, caps = nil, nil
switch = nil
letter = nil
parta, partb = nil, nil
curtemp, lettemp, temp = nil, nil, nil
BGBT, BGTT = nil, nil
offset = nil


And here's my modified MAIN:

-- set up screens for text output
SCREEN_BOTTOM   = 0
SCREEN_TOP   = 1
BGBotText = Screen.LoadTextBG()
Screen.Initialize( SCREEN_BOTTOM, BGBotText )

function listDirectory(path)
   directoryList = {}
   fileList = {}
   if(Directory.Open(path) == 1)then
      Directory.Reset()
      CurrentDirectory = Directory.Next()
      while(string.len(CurrentDirectory) > 0)do
         if(Directory.IsDir() == 0) then
            table.insert(fileList, CurrentDirectory)
         else
            table.insert(directoryList, CurrentDirectory)
         end
         CurrentDirectory = Directory.Next()
      end
      Directory.Close()
   end
   table.sort(directoryList, function(a,b) return string.lower(a)<string.lower(b) end)
   table.sort(fileList, function(a,b) return string.lower(a)<string.lower(b) end)
   return directoryList,fileList
end

currentDirectory = "/"
filepath = '/'

dirs, files = listDirectory(currentDirectory)
pos=0
lastpos=not pos
line=0
dirCount = 0
fileCount = 0

function changeDirectory(directory)
   currentDirectory = directory
   Directory.Chdir(currentDirectory .. "/")
   dirs, files = listDirectory(currentDirectory)
   pos=0
   lastpos=not pos
   line=0
   dirCount = 0
   fileCount = 0

   for i,v in ipairs(dirs) do
      dirCount = dirCount + 1
   end

   for i,v in ipairs(files) do
      fileCount = fileCount + 1
   end
end

function stringreverse(str)
    local reversed = "";
    for i = string.len(str), 1, -1 do
        reversed = reversed..string.sub(str,i,i);
    end
    return reversed;
end

function dofile (filename)
   local f = assert(loadfile(filename))
   return f()
end


firstTime = true

while(true)do
   if(firstTime)then
      firstTime = false
      changeDirectory("/")
   end
   if(lastpos == pos)then
   else
      lastpos=pos
      line = pos
      BGBotText:Clear()
      BGBotText:SetColor(6)
      for i,v in ipairs(dirs) do
         if(line >= 0 and line <= 19)then
            BGBotText:PrintXY(0,line," " .. v)
         end
         line=line+1
      end
      for i,v in ipairs(files) do
         if(string.lower(string.sub(v, string.len(v)-3)) == ".lua")then
            BGBotText:SetColor(2)
         else
            BGBotText:SetColor(0)
         end
         if(line >= 0 and line <= 23)then
            BGBotText:PrintXY(0,line," " .. v)
         end
         line=line+1
      end
      BGBotText:PrintXY(0,0,">")
   end
   if(Pads.Down())then
      while(Pads.Down())do
      end
      if(line > 1)then
         pos = pos-1
      end
   end
   if(Pads.Up())then
      while(Pads.Up())do
      end
      if(pos < 0)then
         pos = pos+1
      end
   end
   aActive = false
   bActive = false
   yActive = false
   if(Pads.A() or Pads.B() or Pads.Y())then
      while(Pads.A() or Pads.B() or Pads.Y())do
         if(Pads.A())then
            aActive = true
            bActive = false
            yActive = false
         elseif(Pads.B())then
            bActive = true
            aActive = false
            yActive = false
         elseif(Pads.Y())then
            aActive = false
            bActive = false
            yActive = true
         end
      end
      if(aActive and (-pos < dirCount))then
         for i,v in pairs(dirs) do
            if(i==-pos+1)then
               if(v == ".")then
               elseif(v == "..")then
                  currentDirectory = string.sub(currentDirectory, 1, string.len(currentDirectory) - string.find(stringreverse(currentDirectory), "/"))
                  if(currentDirectory == "")then
                     currentDirectory = "/"
                  end
                  changeDirectory(currentDirectory)
               else
                  if(currentDirectory == "/")then
                     currentDirectory = currentDirectory .. v
                  else
                     currentDirectory = currentDirectory .. "/" .. v
                  end
                  if(currentDirectory == "")then
                     currentDirectory = "/"
                  end
                  changeDirectory(currentDirectory)
                  break
               end
            end
         end
      elseif(bActive and (-pos < dirCount))then--fix this to ignore B in root directory
         currentDirectory = string.sub(currentDirectory, 1, string.len(currentDirectory) - string.find(stringreverse(currentDirectory), "/"))
         if(currentDirectory == "")then
            currentDirectory = "/"
         end
         changeDirectory(currentDirectory)
      elseif(aActive)then
         for i,v in pairs(files) do
            if(i==-pos-dirCount+1)then
               if(string.lower(string.sub(v, string.len(v)-3)) == ".lua")then
                  Screen.ResetAll()
                  dofile(v)
                  Screen.ResetAll()
                  BGBotText = Screen.LoadTextBG()
                  Screen.Initialize( SCREEN_BOTTOM, BGBotText )
                  changeDirectory(currentDirectory)
               end
            end
         end
      elseif(yActive)then
         for i,v in pairs(files) do
            if(i==-pos-dirCount+1)then
               if(string.lower(string.sub(v, string.len(v)-3)) == ".lua")then
                  Screen.ResetAll()
                  filepath = currentDirectory .. '/' .. v
                  dofile("/SCRIPTS/editlua/editlua.lua")
                  Screen.ResetAll()
                  BGBotText = Screen.LoadTextBG()
                  Screen.Initialize( SCREEN_BOTTOM, BGBotText )
                  changeDirectory(currentDirectory)
               end
            end
         end
      end
   end
end

-- wait until a key is pressed
DSLua.WaitForAnyKey()
Logged
abhuul
New Moon
*
Posts: 4


« Reply #3 on: June 11, 2008, 11:20:49 PM »

sorry for the double post, but is it possible to completely reset the program (DSLua) internally to flush everything before running the MAIN script again?
Logged
Pages: [1]
« previous next »
    Jump to:  



    (C) 2008 DSLua

    DSLua - Best scripting language for Nintendo DS home-brew!