#! /usr/bin/python -O
# -*- python -*-

# Soya 3D
# Copyright (C) 2001-2002 Jean-Baptiste LAMY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Script for converting Soya 0.5 data files to Soya 0.6.
# 
# Just enter your PATH a few lines below, and run the script.
# Your data will no longer be useable with Soya 0.5 of older ;
# please backup them beforme conversion !!!
# 
# Mail me (jiba@tuxfamily.org) if you have troubles with conversion.

import os, sys
import soya, soya.model, soya.soya3d

soya.model.Image.PATH    = '/home/jiba/src/slune/images'
soya.model.Material.PATH = '/home/jiba/src/slune/materials'
soya.soya3d.World.PATH   = '/home/jiba/src/slune/worlds'
soya.model.Shape.PATH    = '/home/jiba/src/slune/shapes'

import copy_reg

def _reconstructor(cls, base, state):
  from soya.math3d import *
  from soya.model  import *
  from soya.soya3d import *
  MAP_CLS = [
    Point,
    Vector,
    Vertex,
    Element3D,
    ]
  for c in MAP_CLS:
    if issubclass(cls, c):
      obj = cls.__new__(cls, state)
      return obj
    
  if base is object:
    obj = object.__new__(cls)
  else:
    obj = base.__new__(cls, state)
    base.__init__(obj, state)
  return obj

copy_reg._reconstructor = _reconstructor




def material_old_setstate(self, state):
  cdata = state["_cdata"]
  del state["_cdata"]
    
  self.__dict__ = state

  # read old cdata
  format = 'Ifffffffffc'
  nb = struct.calcsize(format)
  data = struct.unpack(format, cdata[:nb])

  option = data[0]
  if option & (1<<1): self.separate_specular = 1
  if option & (1<<2): self.additive_blending = 1
  if option & (1<<5): self.clamp = 1

  self.shininess = data[1]
  self.diffuse  = (data[2], data[3], data[4], data[5])
  self.specular = (data[6], data[8], data[8], data[9])

  if data[10] == '\0':
    self.image = None
  else:
    # (String, Width, Height, NbColor)
    format = 'III'
    n = struct.calcsize(format)
    (nb_color, width, height) = struct.unpack(format, cdata[nb:nb + n])
    size = width * height * nb_color * struct.calcsize('B')
    pixels = cdata[nb + n:nb + n + size]
    self.image = _soya._Image(pixels, int(width), int(height), int(nb_color))
    #self.image = soya.model.Image(self.tex_filename)

  if isinstance(self, soya.SavedInAPath):
    if self.filename: self._alls[self.filename] = self

soya.model.Material.__setstate__ = material_old_setstate




soya.init()

files = os.listdir(DIRECTORY)

for file in files:
  if file[-5:] != '.data': continue
  m = soya.model.Material.get(file[:-5])
  m.save()
  print '  Material', m.filename, 'converted'

print '  [DONE]'



ALL = soya.soya3d.World.availables()
print len(ALL)

for file in ALL:
  if file:
    print file
    
    o = soya.soya3d.World.get(file)
    o.save()
    
    
print "Done."


