• Problem with file functions
    3 replies, posted
[CODE]XM = {} local snip = string.Trim // Localize trim function local byte = string.byte // Localize byte function local swap = string.reverse local function lobyte(a) // returns the low byte of a byte local rx = string.format("%X",a) local hret if #rx > 1 then hret = rx[#rx] else hret = rx[1] end return hret end local function hibyte(a) //returns the low byte of a byte. local rx = string.format("%X",a) return rx[1] end local function tohex(a) return string.format("%X",a) end local function doubt(...) local rm = {...} MsgC(Color(100,255,255),"[XM Loader]: ") MsgC(Color(255,255,100),unpack(rm)) MsgC(Color(255,255,100),"\n") end function XM.OpenFile(floc) doubt(string.rep("-",100)) local fobj = file.Open(floc,"r","GAME") // Open in read only mide local function qread(len) return fobj:Read(len) end local function skip(a) fobj:Skip(a) end local function seek(a) fobj:Seek(a) end local function pos() return fobj:Tell() end local function word() return fobj:ReadShort() end local function dword() return fobj:ReadLong() end local function word() return fobj:ReadShort() end local idf = qread(0x11) // ID name. assert((idf~="Extended Module" and idf~="Extended module"),"File is not of correct type.") // Check ID type. local name = snip(qread(0x14)) // Read the name (0x14 bytes) doubt("Track Name: ",name) //doubt(Color(255,0,0),"CURRENT POS ",pos()) skip(1) // $1A here. Not needed, just a term char for terminals. local tracker = qread(0x14) doubt("Tracker Version: ",tracker) local vermin,vermaj = byte(qread(0x2),0x00,0x2) // It has a word size of two, I need the numbers from it. local version = tonumber( hibyte(vermaj) .. ".0" .. lobyte(vermin) ) // The version number is weird. vermin = nil // clean up vermaj = nil // clean up doubt("File version: FastTracker XM-",version) local HEADER_SIZE = dword() //seek(HEADER_SIZE) local SONG_LEN = word() local RESTART_POS = word() local NUM_CHANNELS = word() local NUM_PATTERN = word() local NUM_INSTRUMENTS = word() local I_FLAGS = word() local DEF_TEMPO = word() local DEF_BPM = word() doubt(string.rep("-",100)) doubt(Color(100,255,100), "Header size ", HEADER_SIZE) doubt(Color(100,255,100),"Song length ", SONG_LEN) doubt(Color(100,255,100),"Restart Position ", RESTART_POS) doubt(Color(100,255,100),"Channel Count ", NUM_CHANNELS) doubt(Color(100,255,100),"Pattern Count ", NUM_PATTERN) doubt(Color(100,255,100),"Num Mayonase ", NUM_INSTRUMENTS) doubt(Color(100,255,100),"Flags ", I_FLAGS) doubt(Color(100,255,100),"Default tempo ", DEF_TEMPO) doubt(Color(100,255,100),"Default BPM ", DEF_BPM) end XM.OpenFile("data/expl2.xm") [/CODE] I try to read the file and it just fucks over, it returns bogus data. I'm following the specification down to the tee. For refrence, here's the file i'm using [URL="https://dl.dropboxusercontent.com/u/40443211/dev/expl2.xm"]https://dl.dropboxusercontent.com/u/40443211/dev/expl2.xm[/URL] And here's the specification i'm going off of [URL="https://dl.dropboxusercontent.com/u/40443211/dev/expl2.xm"]https://dl.dropboxusercontent.com/u/40443211/dev/expl2.xm[/URL] Here's the bullshit data its returning [B][I][U] I think its an issue with the file functions. There's data where i'm trying to read from, its just giving me null D:<[/U][/I][/B] [QUOTE] [XM Loader]: ---------------------------------------------------------------------------------------------------- [XM Loader]: Track Name: Rpg-Explosion [XM Loader]: Tracker name FastTracker v2.00 [XM Loader]: XM Version FastTracker-2.04 [XM Loader]: ---------------------------------------------------------------------------------------------------- [XM Loader]: Header size 276 [XM Loader]: Song length 47 [XM Loader]: Restart Position 0 [XM Loader]: Channel Count [XM Loader]: Pattern Count [XM Loader]: Num Mayonase [XM Loader]: Flags [XM Loader]: Default tempo [XM Loader]: Default BPM [/QUOTE] After restart position it starts throwing nulls.
Line 35 should be [lua] local fobj = file.Open(floc,"rb","GAME") // Open in read only mide [/lua] instead.
Shinycow is correct, I also prettified your code if you want it. [lua] XM = {} local trim = string.Trim local byte = string.byte local swap = string.reverse local format = string.format local lower = string.lower local function lobyte(a) // returns the low byte of a byte local rx = format("%X", a) local hret if #rx > 1 then hret = rx[#rx] else hret = rx[1] end return hret end local function hibyte(a) // returns the low byte of a byte. local rx = format("%X", a) return rx[1] end local function tohex(a) return string.format("%X", a) end local col_title = Color(100,255,255) local col_pref = Color(100,255,100) local col_data = Color(255,255,100) function XM.Msg(...) local args = {...} MsgC(col_title, "[XM Loader]: ") MsgC(col_data, unpack(args)) MsgC(col_data,"\n") end function XM.MsgD(key, val) XM.Msg(col_pref, key..":\t", col_data, val) end local function pos(f) return f:Tell() end local function word(f) return f:ReadShort() end local function dword(f) return f:ReadLong() end function XM.OpenFile(floc) XM.Msg(string.rep("-", 100)) local fobj = file.Open(floc, "rb", "GAME") // Open in read-only mode local idf = fobj:Read(17) // ID name (17 bytes) (Extended Module: ) XM.MsgD("ID Name", "\t"..idf) // Indent once more for consistency if (lower(idf) ~= "extended module: ") then XM.Msg("File may not be of correct type!") end // Check ID type local name = trim(fobj:Read(20)) // Read the name (20 bytes) XM.MsgD("Track Name", name) fobj:Skip(1) // 0x1A here. Not needed, just a term char for terminals local tracker = fobj:Read(20) XM.MsgD("Tracker Version", tracker) local vermin, vermaj = byte(fobj:Read(2), 1, 2) // Return the two bytes of version number or something local version = tonumber(hibyte(vermaj) .. ".0" .. lobyte(vermin)) // The version number is weird. XM.MsgD("File version", "FastTracker XM-" .. version) XM.Msg(string.rep("-",100)) XM.MsgD("Header size", byte(fobj:Read(4))) XM.MsgD("Song length", byte(fobj:Read(2))) XM.MsgD("Restart Position", byte(fobj:Read(2))) XM.MsgD("Channel Count", word(fobj)) XM.MsgD("Pattern Count", word(fobj)) XM.MsgD("Num Mayonase", word(fobj)) XM.MsgD("Flags", "\t"..word(fobj)) XM.MsgD("Default tempo", word(fobj)) XM.MsgD("Default BPM", word(fobj)) fobj:Close() end XM.OpenFile("data/expl2.xm") [/lua] Output: [code] [XM Loader]: ---------------------------------------------------------------------------------------------------- [XM Loader]: ID Name: Extended Module: [XM Loader]: Track Name: Rpg-Explosion [XM Loader]: Tracker Version: OpenMPT 1.23.05.00 [XM Loader]: File version: FastTracker XM-1.04 [XM Loader]: ---------------------------------------------------------------------------------------------------- [XM Loader]: Header size: 67 [XM Loader]: Song length: 47 [XM Loader]: Restart Position: 14 [XM Loader]: Channel Count: 26 [XM Loader]: Pattern Count: 39 [XM Loader]: Num Mayonase: 29 [XM Loader]: Flags: 1 [XM Loader]: Default tempo: 4 [XM Loader]: Default BPM: 169 [/code] [editline]25th January 2015[/editline] In the code there are several instances of ="something"> erroneously added by the highlighter for some reason.
[QUOTE=zerf;47008936]Shinycow is correct, I also prettified your code if you want it. [lua] XM = {} local trim = string.Trim local byte = string.byte local swap = string.reverse local format = string.format local lower = string.lower local function lobyte(a) // returns the low byte of a byte local rx = format("%X", a) local hret if #rx > 1 then hret = rx[#rx] else hret = rx[1] end return hret end local function hibyte(a) // returns the low byte of a byte. local rx = format("%X", a) return rx[1] end local function tohex(a) return string.format("%X", a) end local col_title = Color(100,255,255) local col_pref = Color(100,255,100) local col_data = Color(255,255,100) function XM.Msg(...) local args = {...} MsgC(col_title, "[XM Loader]: ") MsgC(col_data, unpack(args)) MsgC(col_data,"\n") end function XM.MsgD(key, val) XM.Msg(col_pref, key..":\t", col_data, val) end local function pos(f) return f:Tell() end local function word(f) return f:ReadShort() end local function dword(f) return f:ReadLong() end function XM.OpenFile(floc) XM.Msg(string.rep("-", 100)) local fobj = file.Open(floc, "rb", "GAME") // Open in read-only mode local idf = fobj:Read(17) // ID name (17 bytes) (Extended Module: ) XM.MsgD("ID Name", "\t"..idf) // Indent once more for consistency if (lower(idf) ~= "extended module: ") then XM.Msg("File may not be of correct type!") end // Check ID type local name = trim(fobj:Read(20)) // Read the name (20 bytes) XM.MsgD("Track Name", name) fobj:Skip(1) // 0x1A here. Not needed, just a term char for terminals local tracker = fobj:Read(20) XM.MsgD("Tracker Version", tracker) local vermin, vermaj = byte(fobj:Read(2), 1, 2) // Return the two bytes of version number or something local version = tonumber(hibyte(vermaj) .. ".0" .. lobyte(vermin)) // The version number is weird. XM.MsgD("File version", "FastTracker XM-" .. version) XM.Msg(string.rep("-",100)) XM.MsgD("Header size", byte(fobj:Read(4))) XM.MsgD("Song length", byte(fobj:Read(2))) XM.MsgD("Restart Position", byte(fobj:Read(2))) XM.MsgD("Channel Count", word(fobj)) XM.MsgD("Pattern Count", word(fobj)) XM.MsgD("Num Mayonase", word(fobj)) XM.MsgD("Flags", "\t"..word(fobj)) XM.MsgD("Default tempo", word(fobj)) XM.MsgD("Default BPM", word(fobj)) fobj:Close() end XM.OpenFile("data/expl2.xm") [/lua] Output: [code] [XM Loader]: ---------------------------------------------------------------------------------------------------- [XM Loader]: ID Name: Extended Module: [XM Loader]: Track Name: Rpg-Explosion [XM Loader]: Tracker Version: OpenMPT 1.23.05.00 [XM Loader]: File version: FastTracker XM-1.04 [XM Loader]: ---------------------------------------------------------------------------------------------------- [XM Loader]: Header size: 67 [XM Loader]: Song length: 47 [XM Loader]: Restart Position: 14 [XM Loader]: Channel Count: 26 [XM Loader]: Pattern Count: 39 [XM Loader]: Num Mayonase: 29 [XM Loader]: Flags: 1 [XM Loader]: Default tempo: 4 [XM Loader]: Default BPM: 169 [/code] [editline]25th January 2015[/editline] In the code there are several instances of ="something"> erroneously added by the highlighter for some reason.[/QUOTE] Thanks! This helped tons :D
Sorry, you need to Log In to post a reply to this thread.