Class: Resedit::Changeable

Inherits:
Object
  • Object
show all
Defined in:
lib/resedit/classes/changeable.rb

Direct Known Subclasses

ExeBody, ExeHeader

Defined Under Namespace

Classes: Change

Constant Summary collapse

HOW_CHANGED =
0
HOW_ORIGINAL =
1
COL_CHANGED =
Colorizer::YELLOW
COL_ORIGINAL =
Colorizer::PURPLE
LOG =
Logger.new(STDOUT)

Instance Method Summary collapse

Constructor Details

#initialize(fileOrBytes, fileSize = nil) ⇒ Changeable

Changeable



175
176
177
178
179
180
181
# File 'lib/resedit/classes/changeable.rb', line 175

def initialize(fileOrBytes, fileSize=nil)
    LOG.level = Logger::INFO
    @col = Colorizer.instance()
    @mode = HOW_CHANGED
    @root = nil
    addData(fileOrBytes, fileSize)
end

Instance Method Details

#addData(fileOrBytes, size = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/resedit/classes/changeable.rb', line 183

def addData(fileOrBytes, size=nil)
    if fileOrBytes.is_a?(IO)
        data = fileOrBytes.read(size)
    else
        data = fileOrBytes
    end
    data = @root.buf + data if @root
    @root = Change.new(data)
end

#bytesObject



236
# File 'lib/resedit/classes/changeable.rb', line 236

def bytes; return @root.all() end

#change(ofs, bytes) ⇒ Object



212
213
214
215
216
# File 'lib/resedit/classes/changeable.rb', line 212

def change(ofs, bytes)
    @root.change(ofs, bytes)
    @root.normalize()
    return ofs
end

#changed?(ofs, size = 1) ⇒ Boolean

Returns:

  • (Boolean)


223
# File 'lib/resedit/classes/changeable.rb', line 223

def changed?(ofs, size=1); return @root.changed?(ofs, size) end

#colStr(str, cond = true) ⇒ Object



259
260
261
262
263
# File 'lib/resedit/classes/changeable.rb', line 259

def colStr(str, cond=true)
    str = sprintf("%04X", str) if !str.is_a?(String)
    return str if !cond
    return @col.color(curcol(), str)
end

#colVal(ofs, size) ⇒ Object



253
254
255
256
257
# File 'lib/resedit/classes/changeable.rb', line 253

def colVal(ofs, size)
    fmt = "%#{size*2}.#{size*2}X"
    u = size == 2 ? "v" : V
    return colStr( sprintf(fmt, getData(ofs, size).unpack(u)[0]) , changed?(ofs,size))
end

#curcolObject



251
# File 'lib/resedit/classes/changeable.rb', line 251

def curcol; @mode == HOW_ORIGINAL ? COL_ORIGINAL : COL_CHANGED end

#dbgdumpObject



229
230
231
232
# File 'lib/resedit/classes/changeable.rb', line 229

def dbgdump
    LOG.debug("---#{@root.size()}---\n")
    @root.dump()
end

#debugObject



225
# File 'lib/resedit/classes/changeable.rb', line 225

def debug(); LOG.level = Logger::DEBUG end

#fix(ofs, bytes) ⇒ Object



218
219
220
221
# File 'lib/resedit/classes/changeable.rb', line 218

def fix(ofs, bytes)
    @root.change(ofs, bytes, true)
    return ofs
end

#getChangesObject



238
# File 'lib/resedit/classes/changeable.rb', line 238

def getChanges; @root.getChanges(); end

#getData(ofs, size) ⇒ Object



234
# File 'lib/resedit/classes/changeable.rb', line 234

def getData(ofs, size); return @root.data(ofs, size) end

#hex(writer, ofs, size, how) ⇒ Object



274
275
276
277
278
# File 'lib/resedit/classes/changeable.rb', line 274

def hex(writer, ofs, size, how)
    mode(parseHow(how))
    col = curcol()
    return @root.hex(writer, ofs, size, col)
end

#hexify(bts) ⇒ Object



299
300
301
# File 'lib/resedit/classes/changeable.rb', line 299

def hexify(bts)
    bts.each_byte.map { |b| sprintf("%02X",b) }.join
end

#insert(offset, data) ⇒ Object



201
202
203
204
205
# File 'lib/resedit/classes/changeable.rb', line 201

def insert(offset, data)
    return if !data || !data.length
    @root.insert(offset, data)
    @root.normalize()
end

#loadChanges(hs) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/resedit/classes/changeable.rb', line 320

def loadChanges(hs)
    mode(HOW_CHANGED)
    @root.revert
    hs.each{|ofs, obj|
        if obj['insert']
            len = obj['insert']
            @root.cload(ofs.to_i, "", len)
        elsif obj['change']
            bts = unhexify(obj['change'])
            @root.cload(ofs.to_i, bts, bts.length)
        end
    }
    mode(HOW_CHANGED)
end

#mode(how) ⇒ Object



195
196
197
198
199
# File 'lib/resedit/classes/changeable.rb', line 195

def mode(how)
    return if @mode==how
    @root.mode(how)
    @mode = how
end

#parseHow(how) ⇒ Object



266
267
268
269
270
271
# File 'lib/resedit/classes/changeable.rb', line 266

def parseHow(how)
    return HOW_CHANGED if !how || how == HOW_CHANGED
    return HOW_ORIGINAL if how == HOW_ORIGINAL
    return HOW_ORIGINAL if how[0] == 'o' || how[0] == 'O'
    return HOW_CHANGED
end


280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/resedit/classes/changeable.rb', line 280

def print(what, how)
    mode(parseHow(how))
    if what=="changes"
        getChanges().each{|ofs,bts|
            printf("%08X: %s -> %s\n", ofs, bts[0].bytes.map { |b| sprintf("%02X",b) }.join,
                 colStr(bts[1].bytes.map { |b| sprintf("%02X",b) }.join))
        }
        puts
        return true
    end
    return false
end

#revert(what) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/resedit/classes/changeable.rb', line 240

def revert(what)
    if what=='all'
        @root.revert()
        @root.normalize()
        return true
    end
    undo(what)
    return true
end

#saveChangesObject



307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/resedit/classes/changeable.rb', line 307

def saveChanges()
    mode(HOW_CHANGED)
    cfg = {}
    chs = getChanges()
    chs.each{|o,bts|
        obj = {}
        obj["insert"] = bts[1].length if bts[0].length==0
        obj["change"] = hexify(bts[0]) if bts[0].length>0
        cfg[o] = obj
    }
    return cfg
end

#saveData(file) ⇒ Object



294
295
296
297
# File 'lib/resedit/classes/changeable.rb', line 294

def saveData(file)
    mode(HOW_CHANGED)
    file.write(bytes())
end

#setData(data) ⇒ Object



193
# File 'lib/resedit/classes/changeable.rb', line 193

def setData(data); @root = Change.new(data) end

#sizeObject



227
# File 'lib/resedit/classes/changeable.rb', line 227

def size; @root.size end

#undo(offset) ⇒ Object



207
208
209
210
# File 'lib/resedit/classes/changeable.rb', line 207

def undo(offset)
    @root.undo(offset)
    @root.normalize()
end

#unhexify(str) ⇒ Object



303
304
305
# File 'lib/resedit/classes/changeable.rb', line 303

def unhexify(str)
    str.scan(/../).collect { |c| c.to_i(16).chr }.join
end